2018-01-03 11:25:58 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2018 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// 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 as version 3 of the License, or //
|
2019-04-11 12:32:15 +00:00
|
|
|
// (at your option) any later version. //
|
2018-01-03 11:25:58 +00:00
|
|
|
// //
|
|
|
|
// 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 V3 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-11-12 14:05:14 +00:00
|
|
|
#include <chrono>
|
2018-01-03 11:25:58 +00:00
|
|
|
|
2018-01-04 10:11:53 +00:00
|
|
|
#include <QKeySequence>
|
2018-01-04 19:15:33 +00:00
|
|
|
#include <QProcess>
|
2018-01-04 10:11:53 +00:00
|
|
|
|
2018-11-12 14:05:14 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "util/simpleserializer.h"
|
2018-11-13 12:19:54 +00:00
|
|
|
#include "util/timeutil.h"
|
2018-11-12 14:05:14 +00:00
|
|
|
|
2018-01-04 19:15:33 +00:00
|
|
|
Command::Command() :
|
2018-11-12 13:04:16 +00:00
|
|
|
m_currentProcess(nullptr),
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessState(QProcess::NotRunning),
|
|
|
|
m_isInError(false),
|
|
|
|
m_currentProcessError(QProcess::UnknownError),
|
2018-01-04 22:10:05 +00:00
|
|
|
m_hasExited(false),
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessExitCode(0),
|
|
|
|
m_currentProcessExitStatus(QProcess::NormalExit),
|
|
|
|
m_currentProcessPid(0)
|
2018-01-03 11:25:58 +00:00
|
|
|
{
|
2018-11-12 14:05:14 +00:00
|
|
|
m_currentProcessStartTimeStampms = 0;
|
|
|
|
m_currentProcessFinishTimeStampms = 0;
|
2018-01-04 19:15:33 +00:00
|
|
|
|
2018-01-03 11:25:58 +00:00
|
|
|
resetToDefaults();
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:15:33 +00:00
|
|
|
Command::Command(const Command& command) :
|
|
|
|
QObject(),
|
|
|
|
m_group(command.m_group),
|
|
|
|
m_description(command.m_description),
|
|
|
|
m_command(command.m_command),
|
|
|
|
m_argString(command.m_argString),
|
|
|
|
m_key(command.m_key),
|
|
|
|
m_keyModifiers(command.m_keyModifiers),
|
|
|
|
m_associateKey(command.m_associateKey),
|
|
|
|
m_release(command.m_release),
|
2018-11-12 13:04:16 +00:00
|
|
|
m_currentProcess(nullptr),
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessState(QProcess::NotRunning),
|
|
|
|
m_isInError(false),
|
|
|
|
m_currentProcessError(QProcess::UnknownError),
|
2018-01-04 22:10:05 +00:00
|
|
|
m_hasExited(false),
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessExitCode(0),
|
|
|
|
m_currentProcessExitStatus(QProcess::NormalExit),
|
|
|
|
m_currentProcessPid(0)
|
|
|
|
{
|
2018-11-12 14:05:14 +00:00
|
|
|
m_currentProcessStartTimeStampms = 0;
|
|
|
|
m_currentProcessFinishTimeStampms = 0;
|
2018-01-04 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 11:25:58 +00:00
|
|
|
Command::~Command()
|
2018-01-05 00:59:01 +00:00
|
|
|
{
|
|
|
|
if (m_currentProcess)
|
|
|
|
{
|
|
|
|
#if QT_VERSION < 0x051000
|
|
|
|
disconnect(m_currentProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#else
|
|
|
|
disconnect(m_currentProcess, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#endif
|
|
|
|
disconnect(m_currentProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
|
|
|
|
disconnect(m_currentProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
|
|
|
|
m_currentProcess->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 11:25:58 +00:00
|
|
|
|
|
|
|
void Command::resetToDefaults()
|
|
|
|
{
|
|
|
|
m_group = "default";
|
|
|
|
m_description = "no name";
|
|
|
|
m_command = "";
|
|
|
|
m_argString = "";
|
2018-01-04 10:11:53 +00:00
|
|
|
m_key = static_cast<Qt::Key>(0);
|
2018-11-12 13:04:16 +00:00
|
|
|
m_keyModifiers = Qt::NoModifier;
|
2018-01-04 10:11:53 +00:00
|
|
|
m_associateKey = false;
|
|
|
|
m_release = false;
|
2018-01-03 11:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Command::serialize() const
|
|
|
|
{
|
|
|
|
SimpleSerializer s(1);
|
|
|
|
|
|
|
|
s.writeString(1, m_group);
|
|
|
|
s.writeString(2, m_description);
|
|
|
|
s.writeString(3, m_command);
|
|
|
|
s.writeString(4, m_argString);
|
2018-01-04 10:11:53 +00:00
|
|
|
s.writeS32(5, (int) m_key);
|
|
|
|
s.writeS32(6, (int) m_keyModifiers);
|
|
|
|
s.writeBool(7, m_associateKey);
|
|
|
|
s.writeBool(8, m_release);
|
2018-01-03 11:25:58 +00:00
|
|
|
|
|
|
|
return s.final();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Command::deserialize(const QByteArray& data)
|
|
|
|
{
|
|
|
|
SimpleDeserializer d(data);
|
|
|
|
|
|
|
|
if (!d.isValid())
|
|
|
|
{
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.getVersion() == 1)
|
|
|
|
{
|
|
|
|
int tmpInt;
|
|
|
|
|
|
|
|
d.readString(1, &m_group, "default");
|
|
|
|
d.readString(2, &m_description, "no name");
|
|
|
|
d.readString(3, &m_command, "");
|
|
|
|
d.readString(4, &m_argString, "");
|
|
|
|
d.readS32(5, &tmpInt, 0);
|
2018-01-04 10:11:53 +00:00
|
|
|
m_key = static_cast<Qt::Key>(tmpInt);
|
2018-01-03 11:25:58 +00:00
|
|
|
d.readS32(6, &tmpInt, 0);
|
2018-01-04 10:11:53 +00:00
|
|
|
m_keyModifiers = static_cast<Qt::KeyboardModifiers>(tmpInt);
|
|
|
|
d.readBool(7, &m_associateKey, false);
|
|
|
|
d.readBool(8, &m_release, false);
|
2018-01-03 11:25:58 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 10:11:53 +00:00
|
|
|
|
|
|
|
QString Command::getKeyLabel() const
|
|
|
|
{
|
|
|
|
if (m_key == 0)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
else if (m_keyModifiers != Qt::NoModifier)
|
|
|
|
{
|
|
|
|
QString altGrStr = m_keyModifiers & Qt::GroupSwitchModifier ? "Gr " : "";
|
|
|
|
int maskedModifiers = (m_keyModifiers & 0x3FFFFFFF) + ((m_keyModifiers & 0x40000000)>>3);
|
|
|
|
return altGrStr + QKeySequence(maskedModifiers, m_key).toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QKeySequence(m_key).toString();
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:15:33 +00:00
|
|
|
|
|
|
|
void Command::run(const QString& apiAddress, int apiPort, int deviceSetIndex)
|
|
|
|
{
|
|
|
|
if (m_currentProcess)
|
|
|
|
{
|
|
|
|
qWarning("Command::run: process already running");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString args = m_argString;
|
|
|
|
|
|
|
|
if (m_argString.contains("%1"))
|
|
|
|
{
|
|
|
|
args = args.arg(apiAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_argString.contains("%2"))
|
|
|
|
{
|
|
|
|
args.replace("%2", "%1");
|
|
|
|
args = args.arg(apiPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_argString.contains("%3"))
|
|
|
|
{
|
|
|
|
args.replace("%3", "%1");
|
|
|
|
args = args.arg(deviceSetIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentProcessCommandLine = QString("%1 %2").arg(m_command).arg(args);
|
|
|
|
qDebug("Command::run: %s", qPrintable(m_currentProcessCommandLine));
|
|
|
|
|
|
|
|
m_currentProcess = new QProcess(this);
|
|
|
|
m_isInError = false;
|
2018-01-04 22:10:05 +00:00
|
|
|
m_hasExited = false;
|
2018-01-04 19:15:33 +00:00
|
|
|
|
|
|
|
#if QT_VERSION < 0x051000
|
|
|
|
connect(m_currentProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#else
|
|
|
|
connect(m_currentProcess, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#endif
|
|
|
|
connect(m_currentProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
|
|
|
|
connect(m_currentProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
|
|
|
|
|
|
|
|
m_currentProcess->setProcessChannelMode(QProcess::MergedChannels);
|
2018-11-13 12:19:54 +00:00
|
|
|
m_currentProcessStartTimeStampms = TimeUtil::nowms();
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcess->start(m_currentProcessCommandLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::kill()
|
|
|
|
{
|
|
|
|
if (m_currentProcess)
|
|
|
|
{
|
|
|
|
qDebug("Command::kill: %lld", m_currentProcessPid);
|
|
|
|
m_currentProcess->kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QProcess::ProcessState Command::getLastProcessState() const
|
|
|
|
{
|
|
|
|
return m_currentProcessState;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Command::getLastProcessError(QProcess::ProcessError& error) const
|
|
|
|
{
|
|
|
|
if (m_isInError) {
|
|
|
|
error = m_currentProcessError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_isInError;
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:10:05 +00:00
|
|
|
bool Command::getLastProcessExit(int& exitCode, QProcess::ExitStatus& exitStatus) const
|
2018-01-04 19:15:33 +00:00
|
|
|
{
|
2018-01-04 22:10:05 +00:00
|
|
|
if (m_hasExited)
|
2018-01-04 19:15:33 +00:00
|
|
|
{
|
|
|
|
exitCode = m_currentProcessExitCode;
|
|
|
|
exitStatus = m_currentProcessExitStatus;
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:10:05 +00:00
|
|
|
return m_hasExited;
|
2018-01-04 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString& Command::getLastProcessLog() const
|
|
|
|
{
|
|
|
|
return m_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::processStateChanged(QProcess::ProcessState newState)
|
|
|
|
{
|
2018-01-05 01:13:15 +00:00
|
|
|
//qDebug("Command::processStateChanged: %d", newState);
|
2018-01-04 19:15:33 +00:00
|
|
|
if (newState == QProcess::Running) {
|
|
|
|
m_currentProcessPid = m_currentProcess->processId();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentProcessState = newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::processError(QProcess::ProcessError error)
|
|
|
|
{
|
2018-01-05 01:13:15 +00:00
|
|
|
//qDebug("Command::processError: %d state: %d", error, m_currentProcessState);
|
2018-11-13 12:19:54 +00:00
|
|
|
m_currentProcessFinishTimeStampms = TimeUtil::nowms();
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessError = error;
|
|
|
|
m_isInError = true;
|
2018-01-04 22:10:05 +00:00
|
|
|
|
|
|
|
if (m_currentProcessState == QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
m_log = m_currentProcess->readAllStandardOutput();
|
2018-01-04 19:15:33 +00:00
|
|
|
|
|
|
|
#if QT_VERSION < 0x051000
|
|
|
|
disconnect(m_currentProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#else
|
|
|
|
disconnect(m_currentProcess, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
#endif
|
2018-01-04 22:10:05 +00:00
|
|
|
disconnect(m_currentProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
|
|
|
|
disconnect(m_currentProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
|
2018-01-04 19:15:33 +00:00
|
|
|
|
2018-01-05 00:59:01 +00:00
|
|
|
m_currentProcess->deleteLater(); // make sure other threads can still access it until all events have been processed
|
2018-11-12 13:04:16 +00:00
|
|
|
m_currentProcess = nullptr; // for this thread it can assume it was deleted
|
2018-01-04 22:10:05 +00:00
|
|
|
}
|
2018-01-04 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Command::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
2018-01-05 01:13:15 +00:00
|
|
|
//qDebug("Command::processFinished: (%d) %d", exitCode, exitStatus);
|
2018-11-13 12:19:54 +00:00
|
|
|
m_currentProcessFinishTimeStampms = TimeUtil::nowms();
|
2018-01-04 19:15:33 +00:00
|
|
|
m_currentProcessExitCode = exitCode;
|
|
|
|
m_currentProcessExitStatus = exitStatus;
|
2018-01-04 22:10:05 +00:00
|
|
|
m_hasExited = true;
|
2018-01-04 19:15:33 +00:00
|
|
|
m_log = m_currentProcess->readAllStandardOutput();
|
|
|
|
|
|
|
|
#if QT_VERSION < 0x051000
|
2018-01-04 22:10:05 +00:00
|
|
|
disconnect(m_currentProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
2018-01-04 19:15:33 +00:00
|
|
|
#else
|
2018-01-04 22:10:05 +00:00
|
|
|
disconnect(m_currentProcess, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
2018-01-04 19:15:33 +00:00
|
|
|
#endif
|
|
|
|
disconnect(m_currentProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
|
|
|
|
disconnect(m_currentProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
|
|
|
|
|
2018-01-05 00:59:01 +00:00
|
|
|
m_currentProcess->deleteLater(); // make sure other threads can still access it until all events have been processed
|
2018-11-12 13:04:16 +00:00
|
|
|
m_currentProcess = nullptr; // for this thread it can assume it was deleted
|
2018-01-04 19:15:33 +00:00
|
|
|
}
|