OSDN Git Service

When job's are added via "--add-job", the job will now be either started immediately...
[x264-launcher/x264-launcher.git] / src / main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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 "global.h"
23 #include "win_main.h"
24 #include "ipc.h"
25 #include "taskbar7.h"
26
27 //Qt includes
28 #include <QApplication>
29 #include <QDate>
30 #include <QPlastiqueStyle>
31
32 //Windows includes
33 #define NOMINMAX
34 #define WIN32_LEAN_AND_MEAN
35 #include <Windows.h>
36
37 //Forward declaration
38 void handleMultipleInstances(QStringList args, IPC *ipc);
39
40 ///////////////////////////////////////////////////////////////////////////////
41 // Main function
42 ///////////////////////////////////////////////////////////////////////////////
43
44 static int x264_main(int argc, char* argv[])
45 {
46         //Init console
47         x264_init_console(argc, argv);
48
49         //Print version info
50         qDebug("Simple x264 Launcher v%u.%02u.%u - use 64-Bit x264 with 32-Bit Avisynth", x264_version_major(), x264_version_minor(), x264_version_build());
51         qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(x264_version_date().year(),QDate::currentDate().year()));
52         qDebug("Built on %s at %s with %s for Win-%s.\n", x264_version_date().toString(Qt::ISODate).toLatin1().constData(), x264_version_time(), x264_version_compiler(), x264_version_arch());
53         
54         //print license info
55         qDebug("This program is free software: you can redistribute it and/or modify");
56         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
57         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
58
59         //Print warning, if this is a "debug" build
60         if(X264_DEBUG)
61         {
62                 qWarning("---------------------------------------------------------");
63                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
64                 qWarning("---------------------------------------------------------\n"); 
65         }
66
67         //Get CLI arguments
68         const QStringList &arguments = x264_arguments();
69         
70         //Detect CPU capabilities
71         const x264_cpu_t cpuFeatures = x264_detect_cpu_features(arguments);
72         qDebug("   CPU vendor id  :  %s (Intel: %s)", cpuFeatures.vendor, X264_BOOL(cpuFeatures.intel));
73         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
74         qDebug("   CPU signature  :  Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
75         qDebug("CPU capabilities  :  MMX=%s, MMXEXT=%s, SSE=%s, SSE2=%s, SSE3=%s, SSSE3=%s, X64=%s", X264_BOOL(cpuFeatures.mmx), X264_BOOL(cpuFeatures.mmx2), X264_BOOL(cpuFeatures.sse), X264_BOOL(cpuFeatures.sse2), X264_BOOL(cpuFeatures.sse3), X264_BOOL(cpuFeatures.ssse3), X264_BOOL(cpuFeatures.x64));
76         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
77
78         //Initialize the IPC handler class
79         bool firstInstance = false;
80         IPC *ipc = new IPC();
81         if(ipc->initialize(firstInstance))
82         {
83                 if(!firstInstance)
84                 {
85                         qDebug("This is *not* the fist instance -> sending all CLI commands to first instance!");
86                         handleMultipleInstances(arguments, ipc);
87                         X264_DELETE(ipc);
88                         return 0;
89                 }
90         }
91         else
92         {
93                 qWarning("IPC initialization has failed!");
94         }
95
96         //Initialize Qt
97         if(!x264_init_qt(argc, argv))
98         {
99                 return -1;
100         }
101         
102         //Running in portable mode?
103         if(x264_portable())
104         {
105                 qDebug("Application is running in portable mode!\n");
106         }
107
108         //Taskbar init
109         WinSevenTaskbar::init();
110
111         //Set style
112         if(!qApp->arguments().contains("--no-style", Qt::CaseInsensitive))
113         {
114                 qApp->setStyle(new QPlastiqueStyle());
115         }
116
117         //Create Main Window
118         MainWindow *mainWin = new MainWindow(&cpuFeatures, ipc);
119         mainWin->show();
120
121         //Run application
122         int ret = qApp->exec();
123
124         //Taskbar uninit
125         WinSevenTaskbar::init();
126         
127         //Clean up
128         X264_DELETE(mainWin);
129         X264_DELETE(ipc);
130         return ret;
131 }
132
133 ///////////////////////////////////////////////////////////////////////////////
134 // Multi-instance handler
135 ///////////////////////////////////////////////////////////////////////////////
136
137 void handleMultipleInstances(QStringList args, IPC *ipc)
138 {
139         bool commandSent = false;
140         unsigned int flags = 0;
141
142         //Skip the program file name
143         args.takeFirst();
144
145         //Process all command-line arguments
146         while(!args.isEmpty())
147         {
148                 const QString current = args.takeFirst();
149                 if(X264_STRCMP(current, "--add") || X264_STRCMP(current, "--add-file"))
150                 {
151                         commandSent = true;
152                         if(!args.isEmpty())
153                         {
154                                 if(!ipc->sendAsync(IPC_OPCODE_ADD_FILE, QStringList() << args.takeFirst()))
155                                 {
156                                         break;
157                                 }
158                         }
159                         else
160                         {
161                                 qWarning("Argument for '--add-file' is missing!");
162                         }
163                 }
164                 else if(X264_STRCMP(current, "--add-job"))
165                 {
166                         commandSent = true;
167                         if(args.size() >= 3)
168                         {
169                                 const QStringList list = args.mid(0, 3);
170                                 args.erase(args.begin(), args.begin() + 3);
171                                 if(!ipc->sendAsync(IPC_OPCODE_ADD_JOB, list, flags))
172                                 {
173                                         break;
174                                 }
175                         }
176                         else
177                         {
178                                 qWarning("Argument(s) for '--add-job' are missing!");
179                                 args.clear();
180                         }
181                 }
182                 else if(X264_STRCMP(current, "--force-start") || X264_STRCMP(current, "--no-force-start"))
183                 {
184                         const bool bEnabled = X264_STRCMP(current, "--force-start");
185                         flags = bEnabled ? (flags | IPC_FLAG_FORCE_START) : (flags & (~IPC_FLAG_FORCE_START));
186                         if(bEnabled) flags = flags & (~IPC_FLAG_FORCE_ENQUEUE);
187                 }
188                 else if(X264_STRCMP(current, "--force-enqueue") || X264_STRCMP(current, "--no-force-enqueue"))
189                 {
190                         const bool bEnabled = X264_STRCMP(current, "--force-enqueue");
191                         flags = bEnabled ? (flags | IPC_FLAG_FORCE_ENQUEUE) : (flags & (~IPC_FLAG_FORCE_ENQUEUE));
192                         if(bEnabled) flags = flags & (~IPC_FLAG_FORCE_START);
193                 }
194                 else if(!current.startsWith("--"))
195                 {
196                         qWarning("Unknown argument: %s", current.toUtf8().constData());
197                         break;
198                 }
199         }
200
201         //If no argument has been sent yet, send a ping!
202         if(!commandSent)
203         {
204                 ipc->sendAsync(IPC_OPCODE_PING, QStringList());
205         }
206 }
207
208 ///////////////////////////////////////////////////////////////////////////////
209 // Applicaton entry point
210 ///////////////////////////////////////////////////////////////////////////////
211
212 LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo);
213 void x264_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
214
215 static int _main(int argc, char* argv[])
216 {
217         if(X264_DEBUG)
218         {
219                 int iResult = -1;
220                 qInstallMsgHandler(x264_message_handler);
221                 X264_MEMORY_CHECK(x264_main, iResult, argc, argv);
222                 x264_finalization();
223                 return iResult;
224         }
225         else
226         {
227                 int iResult = -1;
228                 try
229                 {
230                         qInstallMsgHandler(x264_message_handler);
231                         iResult = x264_main(argc, argv);
232                         x264_finalization();
233                 }
234                 catch(char *error)
235                 {
236                         fflush(stdout);
237                         fflush(stderr);
238                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
239                         x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
240                 }
241                 catch(int error)
242                 {
243                         fflush(stdout);
244                         fflush(stderr);
245                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
246                         x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
247                 }
248                 catch(...)
249                 {
250                         fflush(stdout);
251                         fflush(stderr);
252                         fprintf(stderr, "\nGURU MEDITATION !!!\n");
253                         x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
254                 }
255                 return iResult;
256         }
257 }
258
259 int main(int argc, char* argv[])
260 {
261         if(X264_DEBUG)
262         {
263                 return _main(argc, argv);
264         }
265         else
266         {
267                 __try
268                 {
269                         SetUnhandledExceptionFilter(x264_exception_handler);
270                         _set_invalid_parameter_handler(x264_invalid_param_handler);
271                         return _main(argc, argv);
272                 }
273                 __except(1)
274                 {
275                         fflush(stdout);
276                         fflush(stderr);
277                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
278                         x264_fatal_exit(L"Unhandeled structured exception error, application will exit!");
279                 }
280         }
281 }