OSDN Git Service

Updated copyright year.
[x264-launcher/x264-launcher.git] / src / thread_startup.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2022 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "thread_startup.h"
23
24 //MUtils
25 #include <MUtils/Global.h>
26
27 //Qt
28 #include <QDir>
29 #include <QElapsedTimer>
30 #include <QProcess>
31
32 //-------------------------------------
33 // Constructor
34 //-------------------------------------
35
36 StarupThread::StarupThread(void)
37 {
38 }
39
40 StarupThread::~StarupThread(void)
41 {
42 }
43
44 //-------------------------------------
45 // Utility functions
46 //-------------------------------------
47
48 QStringList StarupThread::runProcess(const QString &exePath, const QStringList &arguments, const QStringList *const extraPaths)
49 {
50         QProcess process;
51
52         //Get file name
53         const QString fileName = QFileInfo(exePath).fileName().toUpper();
54
55         //Setup process object
56         MUtils::init_process(process, QDir::tempPath(), true, extraPaths);
57
58         //Try to start process
59         process.start(exePath, arguments);
60         if (!process.waitForStarted())
61         {
62                 qWarning("Failed to launch %s -> %s", MUTILS_UTF8(fileName), MUTILS_UTF8(process.errorString()));
63                 return QStringList();
64         }
65
66         //Start the timer
67         QElapsedTimer timer;
68         timer.start();
69
70         //Wait until process has finished
71         QStringList processOutput;
72         while (process.state() != QProcess::NotRunning)
73         {
74                 process.waitForReadyRead(1250);
75                 while (process.canReadLine())
76                 {
77                         const QString line = QString::fromUtf8(process.readLine()).simplified();
78                         if (!line.isEmpty())
79                         {
80                                 processOutput << line;
81                         }
82                 }
83                 if ((process.state() != QProcess::NotRunning) && timer.hasExpired(15000))
84                 {
85                         process.waitForFinished(125);
86                         if (process.state() != QProcess::NotRunning)
87                         {
88                                 qWarning("%s process encountered a deadlock -> aborting now!", MUTILS_UTF8(fileName));
89                                 break;
90                         }
91                 }
92                 else
93                 {
94                         QThread::yieldCurrentThread(); /*yield*/
95                 }
96         }
97
98         //Make sure process has terminated!
99         process.waitForFinished(1250);
100         if (process.state() != QProcess::NotRunning)
101         {
102                 qWarning("%s process still running, going to kill it!", MUTILS_UTF8(fileName));
103                 process.kill();
104                 process.waitForFinished(-1);
105         }
106
107         //Read pending lines
108         while (process.bytesAvailable() > 0)
109         {
110                 const QString line = QString::fromUtf8(process.readLine()).simplified();
111                 if (!line.isEmpty())
112                 {
113                         processOutput << line;
114                 }
115         }
116
117         //Check exit code
118         if (process.exitCode() != 0)
119         {
120                 qWarning("%s failed with code 0x%08X -> discarding all output!", MUTILS_UTF8(fileName), process.exitCode());
121                 return QStringList();
122         }
123
124         return processOutput;
125 }