1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Tool_Abstract.h"
27 #include "JobObject.h"
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
36 #include <QMutexLocker>
38 #include <QProcessEnvironment>
44 quint64 AbstractTool::s_lastLaunchTime = 0ui64;
45 QMutex AbstractTool::s_mutex_startProcess;
46 JobObject *AbstractTool::s_jobObject = NULL;
47 unsigned int AbstractTool::s_jobObjRefCount = 0U;
52 static const unsigned int START_DELAY = 333; //in milliseconds
53 static const quint64 START_DELAY_NANO = START_DELAY * 1000 * 10; //in 100-nanosecond intervals
58 AbstractTool::AbstractTool(void)
60 QMutexLocker lock(&s_mutex_startProcess);
62 if(s_jobObjRefCount < 1U)
64 s_jobObject = new JobObject();
65 s_jobObjRefCount = 1U;
78 AbstractTool::~AbstractTool(void)
80 QMutexLocker lock(&s_mutex_startProcess);
82 if(s_jobObjRefCount >= 1U)
85 if(s_jobObjRefCount < 1U)
87 MUTILS_DELETE(s_jobObject);
93 * Initialize and launch process object
95 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
97 QMutexLocker lock(&s_mutex_startProcess);
99 if(lamexp_current_file_time() <= s_lastLaunchTime)
101 MUtils::OS::sleep_ms(START_DELAY);
104 emit messageLogged(commandline2string(program, args) + "\n");
105 MUtils::init_process(process, QFileInfo(program).absolutePath());
107 process.start(program, args);
109 if(process.waitForStarted())
113 if(!s_jobObject->addProcessToJob(&process))
115 qWarning("Failed to assign process to job object!");
119 lamexp_change_process_priority(&process, -1);
124 emit statusUpdated(0);
125 m_firstLaunch = false;
128 s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
132 emit messageLogged("Process creation has failed :-(");
133 QString errorMsg= process.errorString().trimmed();
134 if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
137 process.waitForFinished(-1);
139 s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
144 * Convert program arguments to single string
146 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
148 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
150 for(int i = 0; i < arguments.count(); i++)
152 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));