2010-12-01 23:14:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2014-01-01 17:05:52 +01:00
|
|
|
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-12-01 23:14:47 +01:00
|
|
|
//
|
|
|
|
// 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 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2010-12-01 23:14:47 +01: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 for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Tool_Abstract.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
2011-04-11 21:55:34 +02:00
|
|
|
#include "Global.h"
|
2013-10-07 00:01:15 +02:00
|
|
|
#include "JobObject.h"
|
2011-04-11 21:55:34 +02:00
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
2014-11-30 21:32:23 +01:00
|
|
|
#include <MUtils/OSSupport.h>
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Qt
|
2010-12-01 23:14:47 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QLibrary>
|
2011-11-22 12:43:32 +01:00
|
|
|
#include <QProcessEnvironment>
|
|
|
|
#include <QDir>
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
2014-12-05 22:39:31 +01:00
|
|
|
* Job Object
|
2010-12-01 23:14:47 +01:00
|
|
|
*/
|
2014-12-05 22:39:31 +01:00
|
|
|
QScopedPointer<JobObject> AbstractTool::s_jobObject;
|
|
|
|
QMutex AbstractTool::s_jobObjMtx;
|
2014-12-06 01:10:27 +01:00
|
|
|
quint64 AbstractTool::s_jobObjCnt = 0ui64;
|
2014-12-05 22:39:31 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process Timer
|
|
|
|
*/
|
|
|
|
quint64 AbstractTool::s_startProcessTimer = 0ui64;
|
|
|
|
QMutex AbstractTool::s_startProcessMutex;
|
2012-03-01 02:45:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Const
|
|
|
|
*/
|
2014-12-05 23:01:38 +01:00
|
|
|
static const unsigned int START_DELAY = 100U; //in milliseconds
|
2014-12-05 22:39:31 +01:00
|
|
|
static const quint64 START_DELAY_NANO = quint64(START_DELAY) * 10000ui64; //in 100-nanosecond intervals
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
AbstractTool::AbstractTool(void)
|
2014-12-06 01:10:27 +01:00
|
|
|
:
|
|
|
|
m_firstLaunch(true)
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2014-12-05 22:39:31 +01:00
|
|
|
QMutexLocker lock(&s_jobObjMtx);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2014-12-06 01:10:27 +01:00
|
|
|
if(s_jobObjCnt++ == 0)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2014-12-05 22:39:31 +01:00
|
|
|
s_jobObject.reset(new JobObject());
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
AbstractTool::~AbstractTool(void)
|
|
|
|
{
|
2014-12-06 01:10:27 +01:00
|
|
|
QMutexLocker lock(&s_jobObjMtx);
|
|
|
|
|
|
|
|
if(--s_jobObjCnt == 0)
|
|
|
|
{
|
|
|
|
s_jobObject.reset(NULL);
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize and launch process object
|
|
|
|
*/
|
|
|
|
bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
|
|
|
|
{
|
2014-12-05 22:39:31 +01:00
|
|
|
QMutexLocker lock(&s_startProcessMutex);
|
2014-12-05 23:01:38 +01:00
|
|
|
|
|
|
|
quint64 currentFileTime = MUtils::OS::current_file_time();
|
|
|
|
while(currentFileTime < s_startProcessTimer)
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
2014-12-05 23:01:38 +01:00
|
|
|
const quint64 expectedFileTime = s_startProcessTimer;
|
2014-12-05 22:39:31 +01:00
|
|
|
lock.unlock();
|
2014-12-05 23:01:38 +01:00
|
|
|
MUtils::OS::sleep_ms(size_t((expectedFileTime - currentFileTime) / 10000ui64) + 1U);
|
2014-12-05 22:39:31 +01:00
|
|
|
lock.relock();
|
2014-12-05 23:01:38 +01:00
|
|
|
currentFileTime = MUtils::OS::current_file_time();
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
emit messageLogged(commandline2string(program, args) + "\n");
|
2014-11-25 02:14:42 +01:00
|
|
|
MUtils::init_process(process, QFileInfo(program).absolutePath());
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
process.start(program, args);
|
|
|
|
|
|
|
|
if(process.waitForStarted())
|
|
|
|
{
|
2014-12-06 01:10:27 +01:00
|
|
|
if(!s_jobObject.isNull())
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2013-10-07 00:01:15 +02:00
|
|
|
if(!s_jobObject->addProcessToJob(&process))
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
|
|
|
qWarning("Failed to assign process to job object!");
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
2013-10-07 00:01:15 +02:00
|
|
|
|
2014-12-04 00:02:42 +01:00
|
|
|
MUtils::OS::change_process_priority(&process, -1);
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
if(m_firstLaunch)
|
|
|
|
{
|
|
|
|
emit statusUpdated(0);
|
|
|
|
m_firstLaunch = false;
|
|
|
|
}
|
|
|
|
|
2014-12-05 22:39:31 +01:00
|
|
|
s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
|
2010-12-01 23:14:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-15 21:32:47 +02:00
|
|
|
emit messageLogged("Process creation has failed :-(");
|
|
|
|
QString errorMsg= process.errorString().trimmed();
|
|
|
|
if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
|
|
|
|
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2014-12-05 22:39:31 +01:00
|
|
|
s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
|
2010-12-01 23:14:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert program arguments to single string
|
|
|
|
*/
|
|
|
|
QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
|
|
|
|
{
|
|
|
|
QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
|
|
|
|
|
|
|
|
for(int i = 0; i < arguments.count(); i++)
|
|
|
|
{
|
|
|
|
commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return commandline;
|
|
|
|
}
|
|
|
|
|
|
|
|
|