From 6cf9dfaa682b6fc19d40baf7070124ea927173d7 Mon Sep 17 00:00:00 2001 From: Murat Seker Date: Mon, 29 Jun 2020 22:55:12 +0200 Subject: [PATCH] refactor: Get process IDs by iterating /proc. Improve readability in JsonConnection option handling. (#843) Co-authored-by: brindosch --- src/hyperion-aml/hyperion-aml.cpp | 1 - src/hyperion-remote/JsonConnection.cpp | 92 ++++++------------------- src/hyperion-v4l2/ScreenshotHandler.cpp | 4 +- src/hyperion-x11/hyperion-x11.cpp | 4 +- src/hyperiond/detectProcess.h | 55 ++++++++------- src/hyperiond/main.cpp | 5 +- src/hyperiond/systray.cpp | 6 +- 7 files changed, 60 insertions(+), 107 deletions(-) diff --git a/src/hyperion-aml/hyperion-aml.cpp b/src/hyperion-aml/hyperion-aml.cpp index eb897d8e..9256181a 100644 --- a/src/hyperion-aml/hyperion-aml.cpp +++ b/src/hyperion-aml/hyperion-aml.cpp @@ -1,5 +1,4 @@ - // QT includes #include #include diff --git a/src/hyperion-remote/JsonConnection.cpp b/src/hyperion-remote/JsonConnection.cpp index ad2b7514..ab7428de 100644 --- a/src/hyperion-remote/JsonConnection.cpp +++ b/src/hyperion-remote/JsonConnection.cpp @@ -121,7 +121,7 @@ void JsonConnection::setImage(QImage &image, int priority, int duration) void JsonConnection::setEffect(const QString &effectName, const QString & effectArgs, int priority, int duration) { - qDebug() << "Start effect " << effectName; + qDebug() << "Start effect " << effectName; // create command QJsonObject command, effect; @@ -157,7 +157,7 @@ void JsonConnection::setEffect(const QString &effectName, const QString & effect void JsonConnection::createEffect(const QString &effectName, const QString &effectScript, const QString & effectArgs) { - qDebug() << "Create effect " << effectName; + qDebug() << "Create effect " << effectName; // create command QJsonObject effect; @@ -185,7 +185,7 @@ void JsonConnection::createEffect(const QString &effectName, const QString &effe void JsonConnection::deleteEffect(const QString &effectName) { - qDebug() << "Delete effect configuration" << effectName; + qDebug() << "Delete effect configuration" << effectName; // create command QJsonObject effect; @@ -372,7 +372,6 @@ void JsonConnection::setConfig(const QString &jsonString) command["command"] = QString("config"); command["subcommand"] = QString("setconfig"); - if (jsonString.size() > 0) { QJsonObject configObj; @@ -384,9 +383,6 @@ void JsonConnection::setConfig(const QString &jsonString) command["config"] = configObj; } - - - // send command message QJsonObject reply = sendMessage(command); @@ -423,72 +419,26 @@ void JsonConnection::setAdjustment( adjust["id"] = adjustmentId; } - if (redAdjustment.isValid()) - { - QJsonArray red; - red.append(redAdjustment.red()); - red.append(redAdjustment.green()); - red.append(redAdjustment.blue()); - adjust["red"] = red; - } + const auto addAdjustmentIfValid = [&] (const QString & name, const QColor & adjustment) { + if (adjustment.isValid()) + { + QJsonArray color; + color.append(adjustment.red()); + color.append(adjustment.green()); + color.append(adjustment.blue()); + adjust[name] = color; + } + }; - if (greenAdjustment.isValid()) - { - QJsonArray green; - green.append(greenAdjustment.red()); - green.append(greenAdjustment.green()); - green.append(greenAdjustment.blue()); - adjust["green"] = green; - } + addAdjustmentIfValid("red", redAdjustment); + addAdjustmentIfValid("green", greenAdjustment); + addAdjustmentIfValid("blue", blueAdjustment); + addAdjustmentIfValid("cyan", cyanAdjustment); + addAdjustmentIfValid("magenta", magentaAdjustment); + addAdjustmentIfValid("yellow", yellowAdjustment); + addAdjustmentIfValid("white", whiteAdjustment); + addAdjustmentIfValid("black", blackAdjustment); - if (blueAdjustment.isValid()) - { - QJsonArray blue; - blue.append(blueAdjustment.red()); - blue.append(blueAdjustment.green()); - blue.append(blueAdjustment.blue()); - adjust["blue"] = blue; - } - if (cyanAdjustment.isValid()) - { - QJsonArray cyan; - cyan.append(cyanAdjustment.red()); - cyan.append(cyanAdjustment.green()); - cyan.append(cyanAdjustment.blue()); - adjust["cyan"] = cyan; - } - if (magentaAdjustment.isValid()) - { - QJsonArray magenta; - magenta.append(magentaAdjustment.red()); - magenta.append(magentaAdjustment.green()); - magenta.append(magentaAdjustment.blue()); - adjust["magenta"] = magenta; - } - if (yellowAdjustment.isValid()) - { - QJsonArray yellow; - yellow.append(yellowAdjustment.red()); - yellow.append(yellowAdjustment.green()); - yellow.append(yellowAdjustment.blue()); - adjust["yellow"] = yellow; - } - if (whiteAdjustment.isValid()) - { - QJsonArray white; - white.append(whiteAdjustment.red()); - white.append(whiteAdjustment.green()); - white.append(whiteAdjustment.blue()); - adjust["white"] = white; - } - if (blackAdjustment.isValid()) - { - QJsonArray black; - black.append(blackAdjustment.red()); - black.append(blackAdjustment.green()); - black.append(blackAdjustment.blue()); - adjust["black"] = black; - } if (backlightThreshold != nullptr) { adjust["backlightThreshold"] = *backlightThreshold; diff --git a/src/hyperion-v4l2/ScreenshotHandler.cpp b/src/hyperion-v4l2/ScreenshotHandler.cpp index d12f9479..ed2fdbb8 100644 --- a/src/hyperion-v4l2/ScreenshotHandler.cpp +++ b/src/hyperion-v4l2/ScreenshotHandler.cpp @@ -46,7 +46,7 @@ bool ScreenshotHandler::findNoSignalSettings(const Image & image) ColorRgb redThresoldColor = {255,75,75}; ColorRgb greenThresoldColor = {75,255,75}; ColorRgb blueThresoldColor = {75,75,255}; - + QVector redOffsets; QVector redCounts; QVector greenOffsets; @@ -203,7 +203,7 @@ bool ScreenshotHandler::findNoSignalSettings(const Image & image) { std::cout << "WARNING difference between threshold color and the other color components is to small, signal detection might have problems." << std::endl; } - + if (thresholdGreen > thresholdRed && thresholdGreen > thresholdBlue && ((thresholdGreen-thresholdRed) <= 0.5 || (thresholdGreen-thresholdBlue) <= 0.5)) { std::cout << "WARNING difference between threshold color and the other color components is to small, signal detection might have problems." << std::endl; diff --git a/src/hyperion-x11/hyperion-x11.cpp b/src/hyperion-x11/hyperion-x11.cpp index 322c7b53..31d9a2b3 100644 --- a/src/hyperion-x11/hyperion-x11.cpp +++ b/src/hyperion-x11/hyperion-x11.cpp @@ -67,8 +67,8 @@ int main(int argc, char ** argv) parser.isSet(argCropBottom) ? argCropBottom.getInt(parser) : argCropHeight.getInt(parser), argSizeDecimation.getInt(parser)); // decimation - if (!x11Wrapper.displayInit()) - return -1; + if (!x11Wrapper.displayInit()) + return -1; if (parser.isSet(argScreenshot)) { diff --git a/src/hyperiond/detectProcess.h b/src/hyperiond/detectProcess.h index 557c10b9..48476a61 100644 --- a/src/hyperiond/detectProcess.h +++ b/src/hyperiond/detectProcess.h @@ -1,8 +1,11 @@ #pragma once -#include -#include #include +#include +#include +#include +#include +#include #ifdef WIN32 // psapi.h requires windows.h to be included @@ -12,7 +15,6 @@ unsigned int getProcessIdsByProcessName(const char *processName, QStringList &listOfPids) { - // Clear content of returned list of PIDS listOfPids.clear(); @@ -57,33 +59,36 @@ unsigned int getProcessIdsByProcessName(const char *processName, QStringList &li } } - return listOfPids.count(); - #else - // Run pgrep, which looks through the currently running processses and lists the process IDs - // which match the selection criteria to stdout. - QProcess process; - process.start("pgrep", QStringList() << processName); - process.waitForReadyRead(); + QDir dir("/proc"); + dir.setFilter(QDir::Dirs); + dir.setSorting(QDir::Name | QDir::Reversed); - QByteArray bytes = process.readAllStandardOutput(); + for (const QString & pid : dir.entryList()) { + QRegExp regexp("\\d*"); + if (!regexp.exactMatch(pid)) + { + /* Not a number, can not be PID */ + continue; + } - process.terminate(); - process.waitForFinished(); - process.kill(); + QFile cmdline("/proc/" + pid + "/cmdline"); + if (!cmdline.open(QFile::ReadOnly | QFile::Text)) + { + /* Can not open cmdline file */ + continue; + } - // Output is something like "2472\n2323" for multiple instances - if (bytes.isEmpty()) - return 0; - - #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - listOfPids = QString(bytes).split("\n", Qt::SkipEmptyParts); - #else - listOfPids = QString(bytes).split("\n", QString::SkipEmptyParts); - #endif - - return listOfPids.count(); + QTextStream in(&cmdline); + QString command = in.readAll(); + if (command.startsWith(processName)) + { + listOfPids.push_back(pid); + } + } #endif + + return listOfPids.count(); } diff --git a/src/hyperiond/main.cpp b/src/hyperiond/main.cpp index d90f0c5f..99c099e6 100644 --- a/src/hyperiond/main.cpp +++ b/src/hyperiond/main.cpp @@ -153,7 +153,6 @@ int main(int argc, char** argv) Logger::setLogLevel(Logger::WARNING); // check if we are running already an instance - // TODO Do not use pgrep on linux, instead iter /proc // TODO Allow one session per user // http://www.qtcentre.org/threads/44489-Get-Process-ID-for-a-running-application QStringList listOfPids; @@ -196,8 +195,8 @@ int main(int argc, char** argv) BooleanOption & silentOption = parser.add ('s', "silent", "do not print any outputs"); BooleanOption & verboseOption = parser.add ('v', "verbose", "Increase verbosity"); BooleanOption & debugOption = parser.add ('d', "debug", "Show debug messages"); - parser.add (0x0, "desktop", "show systray on desktop"); - parser.add (0x0, "service", "force hyperion to start as console service"); + parser.add (0x0, "desktop", "show systray on desktop"); + parser.add (0x0, "service", "force hyperion to start as console service"); Option & exportEfxOption = parser.add