OSDN Git Service

Moved some more functions into MUtilities libraries.
[x264-launcher/x264-launcher.git] / src / main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
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.
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 //Internal
23 #include "global.h"
24 #include "win_main.h"
25 #include "cli.h"
26 #include "ipc.h"
27 #include "taskbar7.h"
28 #include "thread_ipc_send.h"
29
30 //MUtils
31 #include <MUtils/Startup.h>
32 #include <MUtils/OSSupport.h>
33 #include <MUtils/CPUFeatures.h>
34 #include <MUtils/IPCChannel.h>
35 #include <MUtils/Version.h>
36
37 //Qt includes
38 #include <QApplication>
39 #include <QDate>
40 #include <QPlastiqueStyle>
41
42 //Windows includes
43 #define NOMINMAX
44 #define WIN32_LEAN_AND_MEAN
45 #include <Windows.h>
46
47 ///////////////////////////////////////////////////////////////////////////////
48 // Helper functions
49 ///////////////////////////////////////////////////////////////////////////////
50
51 static void x264_print_logo(void)
52 {
53         //Print version info
54         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());
55         qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::app_build_date().year(), MUtils::OS::current_date().year()));
56         qDebug("Built on %s at %s with %s for Win-%s.\n", MUTILS_UTF8(MUtils::Version::app_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::app_build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_arch());
57
58         //print license info
59         qDebug("This program is free software: you can redistribute it and/or modify");
60         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
61         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
62
63         //Print library version
64         qDebug("This application is powerd by MUtils library v%u.%02u (%s, %s).\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)));
65
66         //Print warning, if this is a "debug" build
67         if(X264_DEBUG)
68         {
69                 qWarning("---------------------------------------------------------");
70                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
71                 qWarning("---------------------------------------------------------\n"); 
72         }
73 }
74
75 static int x264_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
76 {
77                 int iResult = 0;
78
79         if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
80         {
81                 if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
82                 {
83                         qDebug("Simple x264 Launcher is already running, connecting to running instance...");
84                         QScopedPointer<IPCThread_Send> messageProducerThread(new IPCThread_Send(ipcChannel));
85                         messageProducerThread->start();
86                         if(!messageProducerThread->wait(30000))
87                         {
88                                 qWarning("MessageProducer thread has encountered timeout -> going to kill!");
89                                 messageProducerThread->terminate();
90                                 messageProducerThread->wait();
91                                 MUtils::OS::system_message_err(L"Simple x264 Launcher", L"Simple x264 Launcher is already running, but the running instance doesn't respond!");
92                                 return -1;
93                         }
94                         return 0;
95                 }
96                 else
97                 {
98                         qFatal("The IPC initialization has failed!");
99                         return -1;
100                 }
101         }
102
103         return 1;
104 }
105
106 ///////////////////////////////////////////////////////////////////////////////
107 // Main function
108 ///////////////////////////////////////////////////////////////////////////////
109
110 static int simple_x264_main(int &argc, char **argv)
111 {
112         int iResult = -1;
113
114         //Print logo
115         x264_print_logo();
116
117         //Get CLI arguments
118         const MUtils::OS::ArgumentMap &arguments = MUtils::OS::arguments();
119
120         //Enumerate CLI arguments
121         if(!arguments.isEmpty())
122         {
123                 qDebug("Command-Line Arguments:");
124                 foreach(const QString &key, arguments.uniqueKeys())
125                 {
126                         foreach(const QString &val, arguments.values(key))
127                         {
128                                 if(!val.isEmpty())
129                                 {
130                                         qDebug("--%s = \"%s\"", MUTILS_UTF8(key), MUTILS_UTF8(val));
131                                         continue;
132                                 }
133                                 qDebug("--%s", MUTILS_UTF8(key));
134                         }
135                 }
136                 qDebug(" ");
137         }
138
139         //Detect CPU capabilities
140         const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect();
141         qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.vendor, MUTILS_BOOL2STR(cpuFeatures.intel));
142         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
143         qDebug("   CPU signature  :  Family=%d Model=%d Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
144         qDebug("CPU capabilities  :  MMX=%s SSE=%s SSE2=%s SSE3=%s SSSE3=%s SSE4=%s SSE4.2=%s x64=%s", MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_MMX), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE2), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE4), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE42), MUTILS_BOOL2STR(cpuFeatures.x64));
145         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
146
147         //Initialize Qt
148         QScopedPointer<QApplication> application(MUtils::Startup::create_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")));
149         if(application.isNull())
150         {
151                 return EXIT_FAILURE;
152         }
153
154         //Initialize application
155         application->setWindowIcon(QIcon(":/icons/movie.ico"));
156         application->setApplicationVersion(QString().sprintf("%d.%02d.%04d", x264_version_major(), x264_version_minor(), x264_version_build())); 
157
158         //Initialize the IPC handler class
159         QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("simple-x264-launcher", x264_version_build(), "instance"));
160         if((iResult = x264_initialize_ipc(ipcChannel.data())) < 1)
161         {
162                 return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
163         }
164
165         //Running in portable mode?
166         if(x264_portable())
167         {
168                 qDebug("Application is running in portable mode!\n");
169         }
170
171         //Taskbar init
172         WinSevenTaskbar::init();
173
174         //Set style
175         if(!arguments.contains(CLI_PARAM_NO_GUI_STYLE))
176         {
177                 qApp->setStyle(new QPlastiqueStyle());
178         }
179
180         //Create Main Window
181         QScopedPointer<MainWindow> mainWindow(new MainWindow(cpuFeatures, ipcChannel.data()));
182         mainWindow->show();
183
184         //Run application
185         int ret = qApp->exec();
186
187         //Taskbar uninit
188         WinSevenTaskbar::init();
189         
190         //Exit program
191         return ret;
192 }
193
194 ///////////////////////////////////////////////////////////////////////////////
195 // Applicaton entry point
196 ///////////////////////////////////////////////////////////////////////////////
197
198 int main(int argc, char* argv[])
199 {
200         return MUtils::Startup::startup(argc, argv, simple_x264_main, "Simple x264 Launcher", true /*lamexp_version_demo()*/);
201 }