2010-12-01 23:14:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2012-01-02 00:52:27 +01:00
|
|
|
// Copyright (C) 2004-2012 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
|
|
|
|
// (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, 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"
|
|
|
|
|
2011-04-11 21:55:34 +02:00
|
|
|
#include "Global.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Win32 API definitions
|
|
|
|
*/
|
|
|
|
typedef HANDLE (WINAPI *CreateJobObjectFun)(__in_opt LPSECURITY_ATTRIBUTES lpJobAttributes, __in_opt LPCSTR lpName);
|
|
|
|
typedef BOOL (WINAPI *SetInformationJobObjectFun)(__in HANDLE hJob, __in JOBOBJECTINFOCLASS JobObjectInformationClass, __in_bcount(cbJobObjectInformationLength) LPVOID lpJobObjectInformation, __in DWORD cbJobObjectInformationLength);
|
|
|
|
typedef BOOL (WINAPI *AssignProcessToJobObjectFun)(__in HANDLE hJob, __in HANDLE hProcess);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Static vars
|
|
|
|
*/
|
2012-04-06 23:15:58 +02:00
|
|
|
quint64 AbstractTool::s_lastLaunchTime = 0ui64;
|
|
|
|
QMutex AbstractTool::s_mutex_startProcess;
|
|
|
|
HANDLE AbstractTool::s_handle_jobObject = NULL;
|
|
|
|
unsigned int AbstractTool::s_jobObjRefCount = 0U;
|
2012-03-01 02:45:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Const
|
|
|
|
*/
|
|
|
|
static const DWORD START_DELAY = 333; //in milliseconds
|
|
|
|
static const quint64 START_DELAY_NANO = START_DELAY * 1000 * 10; //in 100-nanosecond intervals
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
AbstractTool::AbstractTool(void)
|
|
|
|
{
|
|
|
|
static CreateJobObjectFun CreateJobObjectPtr = NULL;
|
|
|
|
static SetInformationJobObjectFun SetInformationJobObjectPtr = NULL;
|
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
if(s_jobObjRefCount < 1U)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2012-09-09 23:05:00 +02:00
|
|
|
const lamexp_os_version_t *osVersionNo = lamexp_get_os_version();
|
2012-03-31 16:27:37 +02:00
|
|
|
if(LAMEXP_MIN_OS_VER(osVersionNo, 5, 1))
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2012-03-02 14:31:13 +01:00
|
|
|
if((!CreateJobObjectPtr) || (!SetInformationJobObjectPtr))
|
|
|
|
{
|
|
|
|
QLibrary Kernel32Lib("kernel32.dll");
|
|
|
|
CreateJobObjectPtr = (CreateJobObjectFun) Kernel32Lib.resolve("CreateJobObjectA");
|
|
|
|
SetInformationJobObjectPtr = (SetInformationJobObjectFun) Kernel32Lib.resolve("SetInformationJobObject");
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
if(CreateJobObjectPtr && SetInformationJobObjectPtr)
|
|
|
|
{
|
2012-03-01 02:45:21 +01:00
|
|
|
HANDLE jobObject = CreateJobObjectPtr(NULL, NULL);
|
|
|
|
if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
|
|
|
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
|
|
|
|
memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
|
2012-03-01 02:45:21 +01:00
|
|
|
memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION));
|
2010-12-01 23:14:47 +01:00
|
|
|
jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
|
2012-03-01 02:45:21 +01:00
|
|
|
if(SetInformationJobObjectPtr(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
s_handle_jobObject = jobObject;
|
|
|
|
s_jobObjRefCount = 1U;
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Failed to set job object information!");
|
|
|
|
CloseHandle(jobObject);
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
2012-03-01 23:14:41 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Failed to create the job object!");
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 02:45:21 +01:00
|
|
|
else
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
s_jobObjRefCount++;
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
m_firstLaunch = true;
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
AbstractTool::~AbstractTool(void)
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
if(s_jobObjRefCount >= 1U)
|
2012-02-21 22:36:13 +01:00
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
s_jobObjRefCount--;
|
|
|
|
if((s_jobObjRefCount < 1U) && s_handle_jobObject)
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
CloseHandle(s_handle_jobObject);
|
|
|
|
s_handle_jobObject = NULL;
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2012-02-21 22:36:13 +01:00
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize and launch process object
|
|
|
|
*/
|
|
|
|
bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
|
|
|
|
{
|
|
|
|
static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;
|
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
if(currentTime() <= s_lastLaunchTime)
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
|
|
|
Sleep(START_DELAY);
|
|
|
|
}
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
emit messageLogged(commandline2string(program, args) + "\n");
|
|
|
|
|
2011-11-22 12:43:32 +01:00
|
|
|
QProcessEnvironment env = process.processEnvironment();
|
|
|
|
if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
|
|
|
|
env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
|
|
|
|
env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
|
|
|
|
process.setProcessEnvironment(env);
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
if(!AssignProcessToJobObjectPtr)
|
|
|
|
{
|
|
|
|
QLibrary Kernel32Lib("kernel32.dll");
|
|
|
|
AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject");
|
|
|
|
}
|
|
|
|
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
process.start(program, args);
|
|
|
|
|
|
|
|
if(process.waitForStarted())
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
if(AssignProcessToJobObjectPtr && s_handle_jobObject)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
if(!AssignProcessToJobObjectPtr(s_handle_jobObject, process.pid()->hProcess))
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
|
|
|
qWarning("Failed to assign process to job object!");
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
|
|
|
|
{
|
|
|
|
SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
|
|
|
|
}
|
2010-12-05 23:11:03 +01:00
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
lock.unlock();
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
if(m_firstLaunch)
|
|
|
|
{
|
|
|
|
emit statusUpdated(0);
|
|
|
|
m_firstLaunch = false;
|
|
|
|
}
|
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
s_lastLaunchTime = currentTime() + 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
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
s_lastLaunchTime = currentTime() + 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert long path to short path
|
|
|
|
*/
|
|
|
|
QString AbstractTool::pathToShort(const QString &longPath)
|
|
|
|
{
|
|
|
|
QString shortPath;
|
|
|
|
DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
|
|
|
|
|
|
|
|
if(buffSize > 0)
|
|
|
|
{
|
|
|
|
wchar_t *buffer = new wchar_t[buffSize];
|
|
|
|
DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
|
|
|
|
|
|
|
|
if(result > 0 && result < buffSize)
|
|
|
|
{
|
|
|
|
shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (shortPath.isEmpty() ? longPath : shortPath);
|
|
|
|
}
|
|
|
|
|
2012-03-01 02:45:21 +01:00
|
|
|
const quint64 AbstractTool::currentTime(void)
|
|
|
|
{
|
|
|
|
FILETIME fileTime;
|
|
|
|
GetSystemTimeAsFileTime(&fileTime);
|
|
|
|
|
|
|
|
ULARGE_INTEGER temp;
|
|
|
|
temp.HighPart = fileTime.dwHighDateTime;
|
|
|
|
temp.LowPart = fileTime.dwLowDateTime;
|
|
|
|
|
|
|
|
return temp.QuadPart;
|
|
|
|
}
|