Manually add Windows 10 compat manifest to the installer, to make Aero plug-in work...
[LameXP.git] / src / Tool_Abstract.cpp
blobec5aa7350735ac93b4563cd64507e806aa20f8d6
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Global.h>
30 #include <MUtils/OSSupport.h>
31 #include <MUtils/JobObject.h>
33 //Qt
34 #include <QProcess>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QLibrary>
38 #include <QProcessEnvironment>
39 #include <QDir>
40 #include <QElapsedTimer>
43 * Static Objects
45 QScopedPointer<MUtils::JobObject> AbstractTool::s_jobObjectInstance;
46 QScopedPointer<QElapsedTimer> AbstractTool::s_startProcessTimer;
49 * Synchronization
51 QMutex AbstractTool::s_startProcessMutex;
52 QMutex AbstractTool::s_createObjectMutex;
55 * Ref Counter
57 quint64 AbstractTool::s_referenceCounter = 0ui64;
60 * Const
62 static const qint64 START_DELAY = 64i64; //in milliseconds
65 * Constructor
67 AbstractTool::AbstractTool(void)
69 m_firstLaunch(true)
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!");
86 * Destructor
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)
115 lock.unlock();
116 MUtils::OS::sleep_ms((size_t)(START_DELAY - elapsed));
117 lock.relock();
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);
139 if(m_firstLaunch)
141 emit statusUpdated(0);
142 m_firstLaunch = false;
145 s_startProcessTimer->start();
146 return true;
149 emit messageLogged("Process creation has failed :-(");
150 QString errorMsg= process.errorString().trimmed();
151 if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
153 process.kill();
154 process.waitForFinished(-1);
156 s_startProcessTimer->start();
157 return false;
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)));
172 return commandline;