diff --git a/sources/extsysmon/sources/gpuloadsource.cpp b/sources/extsysmon/sources/gpuloadsource.cpp index 6150bda..c7d1396 100644 --- a/sources/extsysmon/sources/gpuloadsource.cpp +++ b/sources/extsysmon/sources/gpuloadsource.cpp @@ -18,10 +18,9 @@ #include "gpuloadsource.h" +#include #include -#include - #include "awdebug.h" @@ -32,12 +31,23 @@ GPULoadSource::GPULoadSource(QObject *parent, const QStringList args) qCDebug(LOG_ESM) << __PRETTY_FUNCTION__; m_device = args.at(0); + + m_process = new QProcess(nullptr); + // fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished + connect(m_process, + static_cast( + &QProcess::finished), + [this](int, QProcess::ExitStatus) { return updateValue(); }); + m_process->waitForFinished(0); } GPULoadSource::~GPULoadSource() { qCDebug(LOG_ESM) << __PRETTY_FUNCTION__; + + m_process->kill(); + m_process->deleteLater(); } @@ -45,49 +55,10 @@ QVariant GPULoadSource::data(QString source) { qCDebug(LOG_ESM) << "Source" << source; - if (source == QString("gpu/load")) { - float value = 0.0; - if ((m_device != QString("nvidia")) && (m_device != QString("ati"))) - return value; - // build cmd - QString cmd = m_device == QString("nvidia") - ? QString("nvidia-smi -q -x") - : QString("aticonfig --od-getclocks"); - qCInfo(LOG_ESM) << "cmd" << cmd; - TaskResult process = runTask(cmd); - qCInfo(LOG_ESM) << "Cmd returns" << process.exitCode; - qCInfo(LOG_ESM) << "Error" << process.error; - // parse - QString qoutput - = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed(); - if (m_device == QString("nvidia")) { - for (auto str : - qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { - if (!str.contains(QString(""))) - continue; - QString load = str.remove(QString("")) - .remove(QString("")) - .remove(QChar('%')); - value = load.toFloat(); - break; - } - } else if (m_device == QString("ati")) { - for (auto str : - qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { - if (!str.contains(QString("load"))) - continue; - QString load - = str.split(QChar(' '), QString::SkipEmptyParts)[3].remove( - QChar('%')); - value = load.toFloat(); - break; - } - } - // return - return value; - } + if (source == QString("gpu/load")) + run(); - return QVariant(); + return m_value; } @@ -108,6 +79,20 @@ QVariantMap GPULoadSource::initialData(QString source) const } +void GPULoadSource::run() +{ + if ((m_device != QString("nvidia")) && (m_device != QString("ati"))) + return; + // build cmd + QString cmd = m_device == QString("nvidia") + ? QString("nvidia-smi -q -x") + : QString("aticonfig --od-getclocks"); + qCInfo(LOG_ESM) << "cmd" << cmd; + + m_process->start(cmd); +} + + QStringList GPULoadSource::sources() const { QStringList sources; @@ -115,3 +100,39 @@ QStringList GPULoadSource::sources() const return sources; } + + +void GPULoadSource::updateValue() +{ + qCInfo(LOG_LIB) << "Cmd returns" << m_process->exitCode(); + QString qdebug = QTextCodec::codecForMib(106) + ->toUnicode(m_process->readAllStandardError()) + .trimmed(); + qCInfo(LOG_LIB) << "Error" << qdebug; + QString qoutput = QTextCodec::codecForMib(106) + ->toUnicode(m_process->readAllStandardOutput()) + .trimmed(); + qCInfo(LOG_LIB) << "Output" << qoutput; + + if (m_device == QString("nvidia")) { + for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { + if (!str.contains(QString(""))) + continue; + QString load = str.remove(QString("")) + .remove(QString("")) + .remove(QChar('%')); + m_value = load.toFloat(); + break; + } + } else if (m_device == QString("ati")) { + for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { + if (!str.contains(QString("load"))) + continue; + QString load + = str.split(QChar(' '), QString::SkipEmptyParts)[3].remove( + QChar('%')); + m_value = load.toFloat(); + break; + } + } +} diff --git a/sources/extsysmon/sources/gpuloadsource.h b/sources/extsysmon/sources/gpuloadsource.h index 3ddff3b..38e611b 100644 --- a/sources/extsysmon/sources/gpuloadsource.h +++ b/sources/extsysmon/sources/gpuloadsource.h @@ -23,6 +23,8 @@ #include "abstractextsysmonsource.h" +class QProcess; + class GPULoadSource : public AbstractExtSysMonSource { public: @@ -30,12 +32,17 @@ public: virtual ~GPULoadSource(); QVariant data(QString source); QVariantMap initialData(QString source) const; - void run(){}; + void run(); QStringList sources() const; +private slots: + void updateValue(); + private: // configuration and values QString m_device; + QProcess *m_process = nullptr; + QVariant m_value; }; diff --git a/sources/extsysmon/sources/gputempsource.cpp b/sources/extsysmon/sources/gputempsource.cpp index 3c83258..45cdc4b 100644 --- a/sources/extsysmon/sources/gputempsource.cpp +++ b/sources/extsysmon/sources/gputempsource.cpp @@ -18,10 +18,9 @@ #include "gputempsource.h" +#include #include -#include - #include "awdebug.h" @@ -33,12 +32,23 @@ GPUTemperatureSource::GPUTemperatureSource(QObject *parent, qCDebug(LOG_ESM) << __PRETTY_FUNCTION__; m_device = args.at(0); + + m_process = new QProcess(nullptr); + // fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished + connect(m_process, + static_cast( + &QProcess::finished), + [this](int, QProcess::ExitStatus) { return updateValue(); }); + m_process->waitForFinished(0); } GPUTemperatureSource::~GPUTemperatureSource() { qCDebug(LOG_ESM) << __PRETTY_FUNCTION__; + + m_process->kill(); + m_process->deleteLater(); } @@ -46,47 +56,10 @@ QVariant GPUTemperatureSource::data(QString source) { qCDebug(LOG_ESM) << "Source" << source; - if (source == QString("gpu/temperature")) { - float value = 0.0; - if ((m_device != QString("nvidia")) && (m_device != QString("ati"))) - return value; - // build cmd - QString cmd = m_device == QString("nvidia") - ? QString("nvidia-smi -q -x") - : QString("aticonfig --od-gettemperature"); - qCInfo(LOG_ESM) << "cmd" << cmd; - TaskResult process = runTask(cmd); - qCInfo(LOG_ESM) << "Cmd returns" << process.exitCode; - qCInfo(LOG_ESM) << "Error" << process.error; - // parse - QString qoutput - = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed(); - if (m_device == QString("nvidia")) { - for (auto str : - qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { - if (!str.contains(QString(""))) - continue; - QString temp = str.remove(QString("")) - .remove(QString("C")); - value = temp.toFloat(); - break; - } - } else if (m_device == QString("ati")) { - for (auto str : - qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { - if (!str.contains(QString("Temperature"))) - continue; - QString temp - = str.split(QChar(' '), QString::SkipEmptyParts).at(4); - value = temp.toFloat(); - break; - } - } - // return - return value; - } + if (source == QString("gpu/temperature")) + run(); - return QVariant(); + return m_value; } @@ -107,6 +80,20 @@ QVariantMap GPUTemperatureSource::initialData(QString source) const } +void GPUTemperatureSource::run() +{ + if ((m_device != QString("nvidia")) && (m_device != QString("ati"))) + return; + // build cmd + QString cmd = m_device == QString("nvidia") + ? QString("nvidia-smi -q -x") + : QString("aticonfig --od-gettemperature"); + qCInfo(LOG_ESM) << "cmd" << cmd; + + m_process->start(cmd); +} + + QStringList GPUTemperatureSource::sources() const { QStringList sources; @@ -114,3 +101,36 @@ QStringList GPUTemperatureSource::sources() const return sources; } + + +void GPUTemperatureSource::updateValue() +{ + qCInfo(LOG_LIB) << "Cmd returns" << m_process->exitCode(); + QString qdebug = QTextCodec::codecForMib(106) + ->toUnicode(m_process->readAllStandardError()) + .trimmed(); + qCInfo(LOG_LIB) << "Error" << qdebug; + QString qoutput = QTextCodec::codecForMib(106) + ->toUnicode(m_process->readAllStandardOutput()) + .trimmed(); + qCInfo(LOG_LIB) << "Output" << qoutput; + + if (m_device == QString("nvidia")) { + for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { + if (!str.contains(QString(""))) + continue; + QString temp = str.remove(QString("")) + .remove(QString("C")); + m_value = temp.toFloat(); + break; + } + } else if (m_device == QString("ati")) { + for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { + if (!str.contains(QString("Temperature"))) + continue; + QString temp = str.split(QChar(' '), QString::SkipEmptyParts).at(4); + m_value = temp.toFloat(); + break; + } + } +} diff --git a/sources/extsysmon/sources/gputempsource.h b/sources/extsysmon/sources/gputempsource.h index 3e62bad..fc2d7a0 100644 --- a/sources/extsysmon/sources/gputempsource.h +++ b/sources/extsysmon/sources/gputempsource.h @@ -23,6 +23,8 @@ #include "abstractextsysmonsource.h" +class QProcess; + class GPUTemperatureSource : public AbstractExtSysMonSource { public: @@ -30,12 +32,17 @@ public: virtual ~GPUTemperatureSource(); QVariant data(QString source); QVariantMap initialData(QString source) const; - void run(){}; + void run(); QStringList sources() const; +private slots: + void updateValue(); + private: // configuration and values QString m_device; + QProcess *m_process = nullptr; + QVariant m_value; }; diff --git a/sources/extsysmon/sources/playersource.cpp b/sources/extsysmon/sources/playersource.cpp index 2039e25..4a10ccb 100644 --- a/sources/extsysmon/sources/playersource.cpp +++ b/sources/extsysmon/sources/playersource.cpp @@ -351,11 +351,10 @@ QString PlayerSource::buildString(const QString current, const QString value, << "will be stripped after" << s; int index = value.indexOf(current); - if ((current.isEmpty()) || ((index + s + 1) > value.count())) { + if ((current.isEmpty()) || ((index + s + 1) > value.count()))x return QString("%1").arg(value.left(s), s, QLatin1Char(' ')); - } else { + else return QString("%1").arg(value.mid(index + 1, s), s, QLatin1Char(' ')); - } }