pm-inhibit/pm-inhibit.cpp

141 lines
4.1 KiB
C++

/*
* pm-inhibit - Power Management Inhibition Tool
* Copyright (C) 2022 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QTextStream>
#include <QStringList>
#include <QProcess>
#include <cstdlib>
#include "inhibitor.h"
struct InhibitAction
{
QString program;
QString reason;
QString command;
QStringList arguments;
};
QTextStream cout(stdout);
QTextStream cerr(stderr);
void
die(int code, const QString &message)
{
cerr << QString("%1: %2\n").arg(QCoreApplication::applicationName(), message);
::exit(code);
}
void
printVersion(void)
{
cout << QString("%1 %2\n").arg(QCoreApplication::applicationName(),
QCoreApplication::applicationVersion())
<< "Copyright (c) 2022 Damien Goutte-Gattat\n"
"\n"
"This program is released under the GNU General Public License.\n"
"See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n";
::exit(0);
}
void
printHelp(QCommandLineParser &parser)
{
cout << parser.helpText()
<< QString("\nReport bugs to <%1>.\n").arg(PACKAGE_BUGREPORT);
::exit(0);
}
void
parseCommandLine(InhibitAction *action)
{
QCommandLineParser parser;
parser.setApplicationDescription("Run a command with power "
"management features inhibited.");
const QCommandLineOption helpOption = parser.addHelpOption();
const QCommandLineOption versionOption = parser.addVersionOption();
const QCommandLineOption programOption(QStringList() << "p" << "program",
QString("The name of the program requesting the inhibition "
"(defaults to \"%1\").").arg(action->program), "name");
parser.addOption(programOption);
const QCommandLineOption reasonOption(QStringList() << "r" << "reason",
QString("The reason for requesting inhibition "
"(defaults to \"%1\").").arg(action->reason), "reason");
parser.addOption(reasonOption);
parser.addPositionalArgument("command",
QString("The command to run "
"(defaults to \"%1\")").arg(action->command),
"[command...]");
if ( ! parser.parse(QCoreApplication::arguments()) )
die(1, parser.errorText());
if ( parser.isSet(versionOption) )
printVersion();
if ( parser.isSet(helpOption) )
printHelp(parser);
if ( parser.isSet(programOption) )
action->program = parser.value(programOption);
if ( parser.isSet(reasonOption) )
action->reason = parser.value(reasonOption);
QStringList args = parser.positionalArguments();
if ( args.size() > 0 ) {
action->command = args.at(0);
action->arguments = args.mid(1);
}
}
int
main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
app.setApplicationName("pm-inhibit");
app.setApplicationVersion(VERSION);
InhibitAction action = { "pm-inhibit", "Per user request.", "/bin/sh",
QStringList() };
parseCommandLine(&action);
Inhibitor inhibitor(action.program, action.reason);
if ( ! inhibitor.isInhibited() )
die(1, "Cannot inhibit power management features");
QProcess process;
process.setInputChannelMode(QProcess::ForwardedInputChannel);
process.setProcessChannelMode(QProcess::ForwardedChannels);
process.start(action.command, action.arguments);
process.waitForFinished(-1);
return 0;
}