OSDN Git Service

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