Updated copyright year.
[simple-x264-launcher.git] / src / thread_startup.cpp
blob1d7a17b9435ddba2093f5d1f61ec37e10cebb06e
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2023 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "thread_startup.h"
24 //MUtils
25 #include <MUtils/Global.h>
27 //Qt
28 #include <QDir>
29 #include <QElapsedTimer>
30 #include <QProcess>
32 //-------------------------------------
33 // Constructor
34 //-------------------------------------
36 StarupThread::StarupThread(void)
40 StarupThread::~StarupThread(void)
44 //-------------------------------------
45 // Utility functions
46 //-------------------------------------
48 QStringList StarupThread::runProcess(const QString &exePath, const QStringList &arguments, const QStringList *const extraPaths)
50 QProcess process;
52 //Get file name
53 const QString fileName = QFileInfo(exePath).fileName().toUpper();
55 //Setup process object
56 MUtils::init_process(process, QDir::tempPath(), true, extraPaths);
58 //Try to start process
59 process.start(exePath, arguments);
60 if (!process.waitForStarted())
62 qWarning("Failed to launch %s -> %s", MUTILS_UTF8(fileName), MUTILS_UTF8(process.errorString()));
63 return QStringList();
66 //Start the timer
67 QElapsedTimer timer;
68 timer.start();
70 //Wait until process has finished
71 QStringList processOutput;
72 while (process.state() != QProcess::NotRunning)
74 process.waitForReadyRead(1250);
75 while (process.canReadLine())
77 const QString line = QString::fromUtf8(process.readLine()).simplified();
78 if (!line.isEmpty())
80 processOutput << line;
83 if ((process.state() != QProcess::NotRunning) && timer.hasExpired(15000))
85 process.waitForFinished(125);
86 if (process.state() != QProcess::NotRunning)
88 qWarning("%s process encountered a deadlock -> aborting now!", MUTILS_UTF8(fileName));
89 break;
92 else
94 QThread::yieldCurrentThread(); /*yield*/
98 //Make sure process has terminated!
99 process.waitForFinished(1250);
100 if (process.state() != QProcess::NotRunning)
102 qWarning("%s process still running, going to kill it!", MUTILS_UTF8(fileName));
103 process.kill();
104 process.waitForFinished(-1);
107 //Read pending lines
108 while (process.bytesAvailable() > 0)
110 const QString line = QString::fromUtf8(process.readLine()).simplified();
111 if (!line.isEmpty())
113 processOutput << line;
117 //Check exit code
118 if (process.exitCode() != 0)
120 qWarning("%s failed with code 0x%08X -> discarding all output!", MUTILS_UTF8(fileName), process.exitCode());
121 return QStringList();
124 return processOutput;