File indexing completed on 2024-04-28 16:45:05

0001 /*
0002     SPDX-FileCopyrightText: 2012 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <unistd.h>
0008 
0009 #include <KAboutData>
0010 #include <KLocalizedString>
0011 #include <QCommandLineParser>
0012 #include <QDateTime>
0013 #include <QGuiApplication>
0014 #include <QProcess>
0015 
0016 #include <kscreen/config.h>
0017 #include <kscreen/getconfigoperation.h>
0018 
0019 #include "console.h"
0020 
0021 using namespace std;
0022 
0023 void configReceived(KScreen::ConfigOperation *op)
0024 {
0025     const KScreen::ConfigPtr config = qobject_cast<KScreen::GetConfigOperation *>(op)->config();
0026 
0027     const QString command = op->property("command").toString();
0028     const qint64 msecs = QDateTime::currentMSecsSinceEpoch() - op->property("start").toLongLong();
0029     qDebug() << "Received config. Took" << msecs << "milliseconds";
0030 
0031     Console *console = new Console(config);
0032 
0033     if (command.isEmpty()) {
0034         console->printConfig();
0035         console->monitorAndPrint();
0036         return;
0037     } else if (command == QLatin1String("monitor")) {
0038         QTextStream(stdout) << "Remember to enable KSRandR or KSRandR11 in kdebugdialog" << Qt::endl;
0039         // Print config so that we have some pivot data
0040         console->printConfig();
0041         console->monitor();
0042         return;
0043         // Do nothing, enable backend output to see debug
0044     } else if (command == QLatin1String("outputs")) {
0045         console->printConfig();
0046     } else if (command == QLatin1String("config")) {
0047         console->printSerializations();
0048     } else if (command == QLatin1String("bug")) {
0049         QTextStream(stdout) << QStringLiteral("\n========================xrandr --verbose==========================\n");
0050         QProcess proc;
0051         proc.setProcessChannelMode(QProcess::MergedChannels);
0052         proc.start(QStringLiteral("xrandr"), QStringList(QStringLiteral("--verbose")));
0053         proc.waitForFinished();
0054         QTextStream(stdout) << proc.readAll().constData();
0055         QTextStream(stdout) << QStringLiteral("\n========================Outputs===================================\n");
0056         console->printConfig();
0057         QTextStream(stdout) << QStringLiteral("\n========================Configurations============================\n");
0058         console->printSerializations();
0059     } else if (command == QLatin1String("json")) {
0060         console->printJSONConfig();
0061     }
0062     delete console;
0063     qApp->quit();
0064 }
0065 
0066 int main(int argc, char *argv[])
0067 {
0068     dup2(1, 2);
0069 
0070     QGuiApplication app(argc, argv);
0071     KAboutData aboutData(QStringLiteral("kscreen-console"),
0072                          i18n("KScreen Console"),
0073                          QStringLiteral("1.0"),
0074                          i18n("KScreen Console"),
0075                          KAboutLicense::GPL,
0076                          i18n("(c) 2012 KScreen Team"));
0077     KAboutData::setApplicationData(aboutData);
0078 
0079     aboutData.addAuthor(i18n("Alejandro Fiestas Olivares"), i18n("Maintainer"), QStringLiteral("afiestas@kde.org"), QStringLiteral("http://www.afiestas.org/"));
0080 
0081     QCommandLineParser parser;
0082     parser.setApplicationDescription(
0083         i18n("KScreen Console is a CLI tool to query KScreen status\n\n"
0084              "Commands:\n"
0085              "  bug             Show information needed for a bug report\n"
0086              "  config          Show KScreen config files\n"
0087              "  outputs         Show output information\n"
0088              "  monitor         Monitor for changes\n"
0089              "  json            Show current KScreen config"));
0090     parser.addHelpOption();
0091     parser.addPositionalArgument(QStringLiteral("command"), i18n("Command to execute"), QStringLiteral("bug|config|outputs|monitor|json"));
0092     parser.addPositionalArgument(QStringLiteral("[args...]"), i18n("Arguments for the specified command"));
0093 
0094     parser.process(app);
0095 
0096     QString command;
0097     if (!parser.positionalArguments().isEmpty()) {
0098         command = parser.positionalArguments().constFirst();
0099     }
0100 
0101     qDebug() << "START: Requesting Config";
0102 
0103     KScreen::GetConfigOperation *op = new KScreen::GetConfigOperation();
0104     op->setProperty("command", command);
0105     op->setProperty("start", QDateTime::currentMSecsSinceEpoch());
0106     QObject::connect(op, &KScreen::GetConfigOperation::finished, op, [&](KScreen::ConfigOperation *op) {
0107         configReceived(op);
0108     });
0109 
0110     app.exec();
0111 }