1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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"
29 #include <MUtils/Global.h>
30 #include <MUtils/OSSupport.h>
31 #include <MUtils/JobObject.h>
36 #include <QMutexLocker>
38 #include <QProcessEnvironment>
40 #include <QElapsedTimer>
45 QScopedPointer<MUtils::JobObject> AbstractTool::s_jobObjectInstance;
46 QScopedPointer<QElapsedTimer> AbstractTool::s_startProcessTimer;
51 QMutex AbstractTool::s_startProcessMutex;
52 QMutex AbstractTool::s_createObjectMutex;
57 quint64 AbstractTool::s_referenceCounter = 0ui64;
62 static const qint64 START_DELAY = 64i64; //in milliseconds
67 AbstractTool::AbstractTool(void)
72 QMutexLocker lock(&s_createObjectMutex);
74 if(s_referenceCounter++ == 0)
76 s_jobObjectInstance.reset(new MUtils::JobObject());
77 s_startProcessTimer.reset(new QElapsedTimer());
78 if(!MUtils::OS::setup_timer_resolution())
80 qWarning("Failed to setup system timer resolution!");
88 AbstractTool::~AbstractTool(void)
90 QMutexLocker lock(&s_createObjectMutex);
92 if(--s_referenceCounter == 0)
94 s_jobObjectInstance.reset(NULL);
95 s_startProcessTimer.reset(NULL);
96 if(!MUtils::OS::reset_timer_resolution())
98 qWarning("Failed to reset system timer resolution!");
104 * Initialize and launch process object
106 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
108 QMutexLocker lock(&s_startProcessMutex);
110 if((!s_startProcessTimer.isNull()) && s_startProcessTimer->isValid())
112 qint64 elapsed = s_startProcessTimer->elapsed();
113 while(elapsed < START_DELAY)
116 MUtils::OS::sleep_ms((size_t)(START_DELAY - elapsed));
118 elapsed = s_startProcessTimer->elapsed();
122 emit messageLogged(commandline2string(program, args) + "\n");
123 MUtils::init_process(process, QFileInfo(program).absolutePath());
125 process.start(program, args);
127 if(process.waitForStarted())
129 if(!s_jobObjectInstance.isNull())
131 if(!s_jobObjectInstance->addProcessToJob(&process))
133 qWarning("Failed to assign process to job object!");
137 MUtils::OS::change_process_priority(&process, -1);
141 emit statusUpdated(0);
142 m_firstLaunch = false;
145 s_startProcessTimer->start();
149 emit messageLogged("Process creation has failed :-(");
150 QString errorMsg= process.errorString().trimmed();
151 if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
154 process.waitForFinished(-1);
156 s_startProcessTimer->start();
161 * Convert program arguments to single string
163 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
165 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
167 for(int i = 0; i < arguments.count(); i++)
169 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));