hyperion.ng/libsrc/hyperion/Hyperion.cpp

659 lines
19 KiB
C++
Raw Normal View History

// STL includes
#include <exception>
#include <sstream>
#include <unistd.h>
// QT includes
#include <QDateTime>
#include <QThread>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QCryptographicHash>
2018-12-27 23:11:32 +01:00
#include <QTimer>
#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QCryptographicHash>
// hyperion include
#include <hyperion/Hyperion.h>
2018-12-27 23:11:32 +01:00
#include <hyperion/MessageForwarder.h>
#include <hyperion/ImageProcessor.h>
#include <hyperion/ColorAdjustment.h>
2018-12-27 23:11:32 +01:00
// utils
#include <utils/hyperion.h>
// Leddevice includes
#include <leddevice/LedDeviceWrapper.h>
2018-12-27 23:11:32 +01:00
#include <hyperion/MultiColorAdjustment.h>
#include "LinearColorSmoothing.h"
// effect engine includes
#include <effectengine/EffectEngine.h>
2018-12-27 23:11:32 +01:00
// Hyperion Daemon
#include <../src/hyperiond/hyperiond.h>
// settingsManagaer
#include <hyperion/SettingsManager.h>
// BGEffectHandler
#include <hyperion/BGEffectHandler.h>
// CaptureControl (Daemon capture)
#include <hyperion/CaptureCont.h>
// Boblight
#include <boblightserver/BoblightServer.h>
Hyperion* Hyperion::_hyperion = nullptr;
2018-12-27 23:11:32 +01:00
Hyperion* Hyperion::initInstance( HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath)
{
if ( Hyperion::_hyperion != nullptr )
throw std::runtime_error("Hyperion::initInstance can be called only one time");
2018-12-27 23:11:32 +01:00
Hyperion::_hyperion = new Hyperion(daemon, instance, configFile, rootPath);
return Hyperion::_hyperion;
}
Hyperion* Hyperion::getInstance()
{
if ( Hyperion::_hyperion == nullptr )
throw std::runtime_error("Hyperion::getInstance used without call of Hyperion::initInstance before");
return Hyperion::_hyperion;
}
2018-12-27 23:11:32 +01:00
Hyperion::Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath)
: _daemon(daemon)
, _settingsManager(new SettingsManager(this, instance, configFile))
, _componentRegister(this)
, _ledString(hyperion::createLedString(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
, _ledStringClone(hyperion::createLedStringClone(getSetting(settings::LEDS).array(), hyperion::createColorOrder(getSetting(settings::DEVICE).object())))
, _imageProcessor(new ImageProcessor(_ledString, this))
, _muxer(_ledString.leds().size())
2018-12-27 23:11:32 +01:00
, _raw2ledAdjustment(hyperion::createLedColorsAdjustment(_ledString.leds().size(), getSetting(settings::COLOR).object()))
, _effectEngine(nullptr)
, _messageForwarder(new MessageForwarder(this))
, _configFile(configFile)
, _rootPath(rootPath)
2018-12-27 23:11:32 +01:00
, _log(Logger::getInstance("HYPERION"))
, _hwLedCount()
, _configHash()
2018-12-27 23:11:32 +01:00
, _ledGridSize(hyperion::getLedLayoutGridSize(getSetting(settings::LEDS).array()))
, _prevCompId(hyperion::COMP_INVALID)
2018-12-27 23:11:32 +01:00
, _ledBuffer(_ledString.leds().size(), ColorRgb::BLACK)
{
if (!_raw2ledAdjustment->verifyAdjustments())
2018-12-27 23:11:32 +01:00
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
2018-12-27 23:11:32 +01:00
// handle hwLedCount
_hwLedCount = qMax(unsigned(getSetting(settings::DEVICE).object()["hardwareLedCount"].toInt(getLedCount())), getLedCount());
2018-12-27 23:11:32 +01:00
// init colororder vector
for (Led& led : _ledString.leds())
{
_ledStringColorOrder.push_back(led.colorOrder);
}
2018-12-31 15:48:29 +01:00
2018-12-27 23:11:32 +01:00
for (Led& led : _ledStringClone.leds())
{
_ledStringColorOrder.insert(_ledStringColorOrder.begin() + led.index, led.colorOrder);
}
2018-12-27 23:11:32 +01:00
// listens for ComponentRegister changes of COMP_ALL to perform core enable/disable actions
connect(&_componentRegister, &ComponentRegister::updatedComponentState, this, &Hyperion::updatedComponentState);
2018-12-27 23:11:32 +01:00
// listen for settings updates of this instance (LEDS & COLOR)
connect(_settingsManager, &SettingsManager::settingsChanged, this, &Hyperion::handleSettingsUpdate);
2018-12-27 23:11:32 +01:00
// set color correction activity state
const QJsonObject color = getSetting(settings::COLOR).object();
2018-12-27 23:11:32 +01:00
// initialize leddevices
QJsonObject ledDevice = getSetting(settings::DEVICE).object();
2018-12-27 23:11:32 +01:00
ledDevice["currentLedCount"] = int(_hwLedCount); // Inject led count info
_ledDeviceWrapper = new LedDeviceWrapper(this);
connect(this, &Hyperion::componentStateChanged, _ledDeviceWrapper, &LedDeviceWrapper::handleComponentState);
connect(this, &Hyperion::ledDeviceData, _ledDeviceWrapper, &LedDeviceWrapper::write);
_ledDeviceWrapper->createLedDevice(ledDevice);
// smoothing
_deviceSmooth = new LinearColorSmoothing(getSetting(settings::SMOOTHING), this);
connect(this, &Hyperion::settingsChanged, _deviceSmooth, &LinearColorSmoothing::handleSettingsUpdate);
2018-12-27 23:11:32 +01:00
// create the effect engine; needs to be initialized after smoothing!
2018-12-31 15:48:29 +01:00
_effectEngine = new EffectEngine(this);
2018-12-27 23:11:32 +01:00
connect(_effectEngine, &EffectEngine::effectListUpdated, this, &Hyperion::effectListUpdated);
// setup config state checks and initial shot
checkConfigState();
if(_fsWatcher.addPath(_configFile))
QObject::connect(&_fsWatcher, &QFileSystemWatcher::fileChanged, this, &Hyperion::checkConfigState);
else
{
2018-12-27 23:11:32 +01:00
_cTimer = new QTimer(this);
Warning(_log,"Filesystem Observer failed for file: %s, use fallback timer", _configFile.toStdString().c_str());
2018-12-27 23:11:32 +01:00
connect(_cTimer, SIGNAL(timeout()), this, SLOT(checkConfigState()));
_cTimer->start(2000);
}
2018-12-27 23:11:32 +01:00
// initial startup effect
hyperion::handleInitialEffect(this, getSetting(settings::FGEFFECT).object());
2018-12-27 23:11:32 +01:00
// handle background effect
_BGEffectHandler = new BGEffectHandler(this);
2018-12-27 23:11:32 +01:00
// create the Daemon capture interface
_captureCont = new CaptureCont(this);
2018-12-27 23:11:32 +01:00
// if there is no startup / background eff and no sending capture interface we probably want to push once BLACK (as PrioMuxer won't emit a prioritiy change)
2013-08-18 13:33:56 +02:00
update();
// boblight, can't live in global scope as it depends on layout
_boblightServer = new BoblightServer(this, getSetting(settings::BOBLSERVER));
connect(this, &Hyperion::settingsChanged, _boblightServer, &BoblightServer::handleSettingsUpdate);
// set unique id
_id = QString(QCryptographicHash::hash(getConfigFileName().toLocal8Bit(),QCryptographicHash::Sha1).toHex());
}
2018-12-27 23:11:32 +01:00
Hyperion::~Hyperion()
{
2018-12-27 23:11:32 +01:00
freeObjects(false);
}
void Hyperion::freeObjects(bool emitCloseSignal)
{
// switch off all leds
clearall(true);
if (emitCloseSignal)
{
emit closing();
}
// delete components on exit of hyperion core
delete _boblightServer;
2018-12-27 23:11:32 +01:00
delete _captureCont;
delete _effectEngine;
delete _raw2ledAdjustment;
delete _messageForwarder;
2018-12-27 23:11:32 +01:00
delete _settingsManager;
delete _ledDeviceWrapper;
}
2018-12-27 23:11:32 +01:00
void Hyperion::handleSettingsUpdate(const settings::type& type, const QJsonDocument& config)
{
2018-12-27 23:11:32 +01:00
if(type == settings::COLOR)
{
const QJsonObject obj = config.object();
// change in color recreate ledAdjustments
delete _raw2ledAdjustment;
_raw2ledAdjustment = hyperion::createLedColorsAdjustment(_ledString.leds().size(), obj);
if (!_raw2ledAdjustment->verifyAdjustments())
{
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
}
}
else if(type == settings::LEDS)
{
QMutexLocker lock(&_changes);
2018-12-27 23:11:32 +01:00
const QJsonArray leds = config.array();
// // lock update()
// _lockUpdate = true;
2018-12-27 23:11:32 +01:00
// stop and cache all running effects, as effects depend heavily on ledlayout
_effectEngine->cacheRunningEffects();
// ledstring, clone, img processor, muxer, ledGridSize (eff engine image based effects), _ledBuffer and ByteOrder of ledstring
_ledString = hyperion::createLedString(leds, hyperion::createColorOrder(getSetting(settings::DEVICE).object()));
_ledStringClone = hyperion::createLedStringClone(leds, hyperion::createColorOrder(getSetting(settings::DEVICE).object()));
_imageProcessor->setLedString(_ledString);
_muxer.updateLedColorsLength(_ledString.leds().size());
_ledGridSize = hyperion::getLedLayoutGridSize(leds);
std::vector<ColorRgb> color(_ledString.leds().size(), ColorRgb{0,0,0});
_ledBuffer = color;
_ledStringColorOrder.clear();
for (Led& led : _ledString.leds())
{
_ledStringColorOrder.push_back(led.colorOrder);
}
for (Led& led : _ledStringClone.leds())
{
_ledStringColorOrder.insert(_ledStringColorOrder.begin() + led.index, led.colorOrder);
}
// handle hwLedCount update
_hwLedCount = qMax(unsigned(getSetting(settings::DEVICE).object()["hardwareLedCount"].toInt(getLedCount())), getLedCount());
// update led count in device
//_ledDeviceWrapper->setLedCount(_hwLedCount);
2018-12-27 23:11:32 +01:00
// change in leds are also reflected in adjustment
delete _raw2ledAdjustment;
_raw2ledAdjustment = hyperion::createLedColorsAdjustment(_ledString.leds().size(), getSetting(settings::COLOR).object());
// start cached effects
_effectEngine->startCachedEffects();
// // unlock
// _lockUpdate = false;
2018-12-27 23:11:32 +01:00
}
else if(type == settings::DEVICE)
{
QMutexLocker lock(&_changes);
// _lockUpdate = true;
QJsonObject dev = config.object();
2018-12-27 23:11:32 +01:00
// handle hwLedCount update
_hwLedCount = qMax(unsigned(dev["hardwareLedCount"].toInt(getLedCount())), getLedCount());
// force ledString update, if device ByteOrder changed
if(_ledDeviceWrapper->getColorOrder() != dev["colorOrder"].toString("rgb"))
2018-12-27 23:11:32 +01:00
{
_ledString = hyperion::createLedString(getSetting(settings::LEDS).array(), hyperion::createColorOrder(dev));
_ledStringClone = hyperion::createLedStringClone(getSetting(settings::LEDS).array(), hyperion::createColorOrder(dev));
_imageProcessor->setLedString(_ledString);
}
/* // reinit led device type on change
if(_device->getActiveDevice() != dev["type"].toString("file").toLower())
{
}
// update led count
_device->setLedCount(_hwLedCount);
*/
// do always reinit until the led devices can handle dynamic changes
2018-12-27 23:11:32 +01:00
dev["currentLedCount"] = int(_hwLedCount); // Inject led count info
_ledDeviceWrapper->createLedDevice(dev);
// _lockUpdate = false;
2018-12-27 23:11:32 +01:00
}
// update once to push single color sets / adjustments/ ledlayout resizes and update ledBuffer color
2018-12-27 23:11:32 +01:00
update();
}
2018-12-27 23:11:32 +01:00
QJsonDocument Hyperion::getSetting(const settings::type& type)
2013-08-14 10:54:49 +02:00
{
2018-12-27 23:11:32 +01:00
return _settingsManager->getSetting(type);
2013-08-14 10:54:49 +02:00
}
2018-12-27 23:11:32 +01:00
bool Hyperion::saveSettings(QJsonObject config, const bool& correct)
{
2018-12-27 23:11:32 +01:00
return _settingsManager->saveSettings(config, correct);
}
2018-12-27 23:11:32 +01:00
QString Hyperion::getConfigFileName() const
{
2018-12-27 23:11:32 +01:00
QFileInfo cF(_configFile);
return cF.fileName();
}
2018-12-27 23:11:32 +01:00
int Hyperion::getLatchTime() const
{
return _ledDeviceWrapper->getLatchTime();
}
2018-12-27 23:11:32 +01:00
unsigned Hyperion::addSmoothingConfig(int settlingTime_ms, double ledUpdateFrequency_hz, unsigned updateDelay)
{
2018-12-27 23:11:32 +01:00
return _deviceSmooth->addConfig(settlingTime_ms, ledUpdateFrequency_hz, updateDelay);
}
2018-12-27 23:11:32 +01:00
unsigned Hyperion::getLedCount() const
{
2018-12-27 23:11:32 +01:00
return _ledString.leds().size();
}
void Hyperion::checkConfigState(QString cfile)
{
// Check config modifications
QFile f(_configFile);
if (f.open(QFile::ReadOnly))
{
QCryptographicHash hash(QCryptographicHash::Sha1);
if (hash.addData(&f))
{
if (_configHash.size() == 0)
{
_configHash = hash.result();
}
_configMod = _configHash != hash.result() ? true : false;
}
}
f.close();
if(_prevConfigMod != _configMod)
{
_prevConfigMod = _configMod;
}
// Check config writeable
QFile file(_configFile);
QFileInfo fileInfo(file);
_configWrite = fileInfo.isWritable() && fileInfo.isReadable() ? true : false;
if(_prevConfigWrite != _configWrite)
{
_prevConfigWrite = _configWrite;
}
}
void Hyperion::setSourceAutoSelectEnabled(bool enabled)
{
2018-12-27 23:11:32 +01:00
if(_muxer.setSourceAutoSelectEnabled(enabled))
update();
}
bool Hyperion::setCurrentSourcePriority(int priority )
{
2018-12-27 23:11:32 +01:00
return _muxer.setPriority(priority);
}
2018-12-27 23:11:32 +01:00
bool Hyperion::sourceAutoSelectEnabled()
{
return _muxer.isSourceAutoSelectEnabled();
}
void Hyperion::setNewComponentState(const hyperion::Components& component, const bool& state)
{
_componentRegister.componentStateChanged(component, state);
}
void Hyperion::setComponentState(const hyperion::Components component, const bool state)
{
emit componentStateChanged(component, state);
}
2018-12-27 23:11:32 +01:00
void Hyperion::registerInput(const int priority, const hyperion::Components& component, const QString& origin, const QString& owner, unsigned smooth_cfg)
{
2018-12-27 23:11:32 +01:00
_muxer.registerInput(priority, component, origin, owner, smooth_cfg);
2013-08-18 13:33:56 +02:00
}
bool Hyperion::setInput(const int priority, const std::vector<ColorRgb>& ledColors, int timeout_ms, const bool& clearEffect)
{
2018-12-27 23:11:32 +01:00
if(_muxer.setInput(priority, ledColors, timeout_ms))
{
2018-12-27 23:11:32 +01:00
// clear effect if this call does not come from an effect
if(clearEffect)
_effectEngine->channelCleared(priority);
2018-12-27 23:11:32 +01:00
// if this priority is visible, update immediately
if(priority == _muxer.getCurrentPriority())
update();
2018-12-27 23:11:32 +01:00
return true;
}
2018-12-27 23:11:32 +01:00
return false;
}
bool Hyperion::setInputImage(const int priority, const Image<ColorRgb>& image, int64_t timeout_ms, const bool& clearEffect)
{
2018-12-27 23:11:32 +01:00
if(_muxer.setInputImage(priority, image, timeout_ms))
{
2018-12-27 23:11:32 +01:00
// clear effect if this call does not come from an effect
if(clearEffect)
_effectEngine->channelCleared(priority);
// if this priority is visible, update immediately
if(priority == _muxer.getCurrentPriority())
update();
return true;
}
2018-12-27 23:11:32 +01:00
return false;
}
bool Hyperion::setInputInactive(const quint8& priority)
{
return _muxer.setInputInactive(priority);
}
2018-12-27 23:11:32 +01:00
void Hyperion::setColor(int priority, const ColorRgb &color, const int timeout_ms, const QString& origin, bool clearEffects)
{
// clear effect if this call does not come from an effect
if(clearEffects)
_effectEngine->channelCleared(priority);
// create led vector from single color
std::vector<ColorRgb> ledColors(_ledString.leds().size(), color);
// register color
registerInput(priority, hyperion::COMP_COLOR, origin);
// write color to muxer
setInput(priority, ledColors, timeout_ms);
}
const QStringList & Hyperion::getAdjustmentIds() const
{
return _raw2ledAdjustment->getAdjustmentIds();
}
ColorAdjustment * Hyperion::getAdjustment(const QString& id)
{
return _raw2ledAdjustment->getAdjustment(id);
}
void Hyperion::adjustmentsUpdated()
{
2018-12-27 23:11:32 +01:00
emit adjustmentChanged();
update();
}
bool Hyperion::clear(int priority)
2013-08-18 13:33:56 +02:00
{
// send clear signal to the effect engine
// (outside the check so the effect gets cleared even when the effect is not sending colors)
_effectEngine->channelCleared(priority);
2018-12-27 23:11:32 +01:00
if(_muxer.clearInput(priority))
return true;
return false;
2013-08-18 13:33:56 +02:00
}
void Hyperion::clearall(bool forceClearAll)
2013-08-18 13:33:56 +02:00
{
_muxer.clearAll(forceClearAll);
// send clearall signal to the effect engine
_effectEngine->allChannelsCleared();
2013-08-18 13:33:56 +02:00
}
int Hyperion::getCurrentPriority() const
{
2018-12-27 23:11:32 +01:00
return _muxer.getCurrentPriority();
}
bool Hyperion::isCurrentPriority(const int priority) const
{
return getCurrentPriority() == priority;
}
QList<int> Hyperion::getActivePriorities() const
{
return _muxer.getPriorities();
}
2018-12-27 23:11:32 +01:00
const Hyperion::InputInfo Hyperion::getPriorityInfo(const int priority) const
{
return _muxer.getInputInfo(priority);
}
bool Hyperion::saveEffect(const QJsonObject& obj, QString& resultMsg)
2018-12-31 15:48:29 +01:00
{
return _effectEngine->saveEffect(obj, resultMsg);
}
bool Hyperion::deleteEffect(const QString& effectName, QString& resultMsg)
{
2018-12-31 15:48:29 +01:00
return _effectEngine->deleteEffect(effectName, resultMsg);
}
const std::list<EffectDefinition> & Hyperion::getEffects() const
{
return _effectEngine->getEffects();
}
const std::list<ActiveEffectDefinition> & Hyperion::getActiveEffects()
{
return _effectEngine->getActiveEffects();
}
const std::list<EffectSchema> & Hyperion::getEffectSchemas()
{
return _effectEngine->getEffectSchemas();
}
2018-12-27 23:11:32 +01:00
const QJsonObject& Hyperion::getQJsonConfig()
{
return _settingsManager->getSettings();
}
int Hyperion::setEffect(const QString &effectName, int priority, int timeout, const QString & origin)
{
return _effectEngine->runEffect(effectName, priority, timeout, origin);
}
int Hyperion::setEffect(const QString &effectName, const QJsonObject &args, int priority, int timeout, const QString &pythonScript, const QString &origin, const QString &imageData)
{
return _effectEngine->runEffect(effectName, args, priority, timeout, pythonScript, origin, 0, imageData);
}
2018-12-27 23:11:32 +01:00
void Hyperion::setLedMappingType(const int& mappingType)
{
if(mappingType != _imageProcessor->getUserLedMappingType())
{
_imageProcessor->setLedMappingType(mappingType);
emit imageToLedsMappingChanged(mappingType);
}
}
const int & Hyperion::getLedMappingType()
{
2018-12-27 23:11:32 +01:00
return _imageProcessor->getUserLedMappingType();
}
2018-12-27 23:11:32 +01:00
void Hyperion::setVideoMode(const VideoMode& mode)
{
emit videoMode(mode);
}
2018-12-27 23:11:32 +01:00
const VideoMode & Hyperion::getCurrentVideoMode()
{
return _daemon->getVideoMode();
}
2018-12-27 23:11:32 +01:00
const QString & Hyperion::getActiveDevice()
{
return _ledDeviceWrapper->getActiveDevice();
2018-12-27 23:11:32 +01:00
}
void Hyperion::updatedComponentState(const hyperion::Components comp, const bool state)
{
QMutexLocker lock(&_changes);
// evaluate comp change
if (comp != _prevCompId)
{
_imageProcessor->setBlackbarDetectDisable((_prevCompId == hyperion::COMP_EFFECT));
_imageProcessor->setHardLedMappingType((_prevCompId == hyperion::COMP_EFFECT) ? 0 : -1);
_prevCompId = comp;
_raw2ledAdjustment->setBacklightEnabled((_prevCompId != hyperion::COMP_COLOR && _prevCompId != hyperion::COMP_EFFECT));
}
if(comp == hyperion::COMP_ALL)
_muxer.setEnable(state); // first muxer to update all inputs
}
void Hyperion::update()
{
QMutexLocker lock(&_changes);
2018-12-27 23:11:32 +01:00
// Obtain the current priority channel
int priority = _muxer.getCurrentPriority();
const PriorityMuxer::InputInfo priorityInfo = _muxer.getInputInfo(priority);
2018-12-20 15:12:45 +01:00
2018-12-27 23:11:32 +01:00
// copy image & process OR copy ledColors from muxer
Image<ColorRgb> image = priorityInfo.image;
if(image.size() > 3)
{
2018-12-27 23:11:32 +01:00
emit currentImage(image);
_ledBuffer = _imageProcessor->process(image);
}
2018-12-27 23:11:32 +01:00
else
_ledBuffer = priorityInfo.ledColors;
2018-12-31 15:48:29 +01:00
// emit rawLedColors before transform
emit rawLedColors(_ledBuffer);
2018-12-27 23:11:32 +01:00
_raw2ledAdjustment->applyAdjustment(_ledBuffer);
// insert cloned leds into buffer
for (Led& led : _ledStringClone.leds())
{
_ledBuffer.insert(_ledBuffer.begin() + led.index, _ledBuffer.at(led.clone));
}
int i = 0;
for (ColorRgb& color : _ledBuffer)
{
// correct the color byte order
switch (_ledStringColorOrder.at(i))
{
case ORDER_RGB:
// leave as it is
break;
case ORDER_BGR:
std::swap(color.red, color.blue);
break;
case ORDER_RBG:
std::swap(color.green, color.blue);
break;
case ORDER_GRB:
std::swap(color.red, color.green);
break;
case ORDER_GBR:
std::swap(color.red, color.green);
std::swap(color.green, color.blue);
break;
case ORDER_BRG:
std::swap(color.red, color.blue);
std::swap(color.green, color.blue);
break;
}
i++;
}
// fill additional hw leds with black
if ( _hwLedCount > _ledBuffer.size() )
{
_ledBuffer.resize(_hwLedCount, ColorRgb::BLACK);
}
// Write the data to the device
if (_ledDeviceWrapper->enabled())
{
_deviceSmooth->selectConfig(priorityInfo.smooth_cfg);
// feed smoothing in pause mode to maintain a smooth transistion back to smooth mode
if (_deviceSmooth->enabled() || _deviceSmooth->pause())
_deviceSmooth->setLedValues(_ledBuffer);
if (! _deviceSmooth->enabled())
emit ledDeviceData(_ledBuffer);
}
}