KDEAmbi/main.cpp

190 lines
7.3 KiB
C++

#include <QCommandLineParser>
#include <signal.h>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QJsonDocument>
#include <QJsonObject>
#ifdef WLED_CONFIG_TOOL_BUILD
#include <QCoreApplication>
#include "wledconfigclient.h"
#else
#include <QApplication>
#include "hyperiongrabber.h"
#endif
#ifndef WLED_CONFIG_TOOL_BUILD
static HyperionGrabber *grab;
static QApplication *qapp;
#endif
static QFile *logFile = nullptr;
void customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString logMessage = QString("%1 %2 %3 %4 %5")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(context.category)
.arg(context.function)
.arg(context.line)
.arg(localMsg.constData());
switch (type) {
case QtDebugMsg:
logMessage = "Debug: " + logMessage;
break;
case QtInfoMsg:
logMessage = "Info: " + logMessage;
break;
case QtWarningMsg:
logMessage = "Warning: " + logMessage;
break;
case QtCriticalMsg:
logMessage = "Critical: " + logMessage;
break;
case QtFatalMsg:
logMessage = "Fatal: " + logMessage;
break;
}
if (logFile && logFile->isOpen()) {
QTextStream stream(logFile);
stream << logMessage << Qt::endl;
stream.flush();
}
if (type == QtCriticalMsg || type == QtFatalMsg || !logFile || !logFile->isOpen()) {
fprintf(stderr, "%s\n", logMessage.toLocal8Bit().constData());
}
}
#ifndef WLED_CONFIG_TOOL_BUILD
static void quit(int)
{
if (grab != nullptr) {
delete grab;
}
if (logFile) {
logFile->close();
delete logFile;
logFile = nullptr;
}
qapp->exit();
}
#endif
#ifdef WLED_CONFIG_TOOL_BUILD
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCommandLineParser parser;
parser.setApplicationDescription("WLED Configuration Tool");
parser.addHelpOption();
parser.addOption(QCommandLineOption({"a", "address"}, "IP address of the WLED device.", "address"));
parser.process(app);
if (!parser.isSet("address")) {
qWarning() << "Error: WLED IP address not provided. Use -a or --address.";
parser.showHelp(1);
}
QString wledIp = parser.value("address");
WledConfigClient client(wledIp);
QObject::connect(&client, &WledConfigClient::infoReceived, [&](const QJsonObject &info) {
QJsonDocument doc(info);
qDebug() << "WLED Configuration Info:\n" << doc.toJson(QJsonDocument::Indented);
app.quit();
});
QObject::connect(&client, &WledConfigClient::error, [&](const QString &message) {
qWarning() << "Error retrieving WLED info:" << message;
app.quit();
});
client.getInfo();
return app.exec();
}
#else
int main(int argc, char *argv[])
{
logFile = new QFile("output.log");
if (!logFile->open(QFile::WriteOnly | QFile::Text | QFile::Truncate)) {
fprintf(stderr, "Warning: Could not open log file output.log for writing.\n");
delete logFile;
logFile = nullptr;
}
qInstallMessageHandler(customMessageOutput);
qapp = new QApplication(argc, argv);
signal(SIGINT, quit);
signal(SIGTERM, quit);
QApplication::setApplicationName("HyperionGrabber");
QApplication::setApplicationVersion("0.2");
QCommandLineParser parser;
parser.setApplicationDescription("Hyperion grabber for Wayland using Qt");
parser.addHelpOption();
parser.addVersionOption();
parser.addOption(QCommandLineOption({"a", "address"}, "Address to the hyperion json server. (ex. 127.0.0.1)", "address"));
parser.addOption(QCommandLineOption({"p", "port"}, "Port for the hyperion json server. (ex. 19444)", "port"));
parser.addOption(QCommandLineOption({"s", "scale"}, "Divisor used to scale your screen resolution (ex. 8 ; if your screen is 1920x1080, the result image sent to hyperion is 240x135", "scale"));
parser.addOption(QCommandLineOption({"f", "frameskip"}, "Number of frames to skip between captures (ex. 1 ; 0 means no frames are skipped)", "frameskip"));
parser.addOption(QCommandLineOption({"i", "inactive"}, "How many seconds after the screen is inactive to turn off the LED's. Set to 0 to disable.", "seonds"));
parser.addOption(QCommandLineOption({"r", "redadjust"}, "Adjustment of the LED's red color (requires 3 space seperated values between 0 and 255) (ex. \"255,10,0\")", "value"));
parser.addOption(QCommandLineOption({"g", "greenadjust"}, "Adjustment of the LED's green color (requires 3 space seperated values between 0 and 255) (ex. \"75,210,0\")", "value"));
parser.addOption(QCommandLineOption({"b", "blueadjust"}, "Adjustment of the LED's blue color (requires 3 space seperated values between 0 and 255) (ex. \"0,10,160\")", "value"));
parser.addOption(QCommandLineOption({"t", "temperature"}, "Adjustment of the LED's color temperature (requires 3 space seperated values between 0 and 255) (ex. \"255,255,250\")", "value"));
parser.addOption(QCommandLineOption({"d", "threshold"}, "Set the threshold of the LED's (requires 3 space seperated values between 0.0 and 1.0) (ex. \"0.0025,0.005,0.01\")", "value"));
parser.addOption(QCommandLineOption({"l", "transform"}, "Adjusts the luminance / saturation of the LED's values are in this order: luminanceGain, luminanceMin, saturationL (requires 3 space seperated values between 0.0 and 1.0) (ex. \"1.0,0.01,1.0\")", "value"));
parser.addOption(QCommandLineOption("flash", "Flash a range of LEDs with a specific color. Usage: --flash <startIndex> <count> <R> <G> <B>", "startIndex count R G B"));
parser.process(*qapp);
#ifndef DEBUG_MODE
if (parser.isSet("flash")) {
QStringList flashArgs = parser.value("flash").split(" ", Qt::SkipEmptyParts);
if (flashArgs.size() == 5) {
int startIndex = flashArgs.at(0).toInt();
int count = flashArgs.at(1).toInt();
int r = flashArgs.at(2).toInt();
int g = flashArgs.at(3).toInt();
int b = flashArgs.at(4).toInt();
// Create a temporary HyperionGrabber to access WledClient
// This is a temporary solution for testing the flash function
// In a real application, you'd want a more robust way to control WledClient
QHash<QString, QString> tempOpts;
// Pass the address and port from command line to tempGrabber
if (parser.isSet("address")) tempOpts.insert("address", parser.value("address"));
if (parser.isSet("port")) tempOpts.insert("port", parser.value("port"));
HyperionGrabber tempGrabber(tempOpts);
tempGrabber._client_p->flashLeds(startIndex, count, QColor(r, g, b));
qDebug() << "Flashing LEDs: startIndex=" << startIndex << ", count=" << count << ", color=" << r << "," << g << "," << b;
return 0; // Exit after flashing
} else {
qWarning() << "Invalid arguments for --flash. Usage: --flash <startIndex> <count> <R> <G> <B>";
return 1;
}
}
#endif
QHash<QString, QString> opts;
for (const auto &optName : parser.optionNames()) {
if (parser.isSet(optName)) {
opts.insert(optName, parser.value(optName));
}
}
grab = new HyperionGrabber(opts);
return qapp->exec();
}
#endif