OSDN Git Service

Happy new year 2016!
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 //LameXP includes
24 #include "Global.h"
25 #include "Dialog_SplashScreen.h"
26 #include "Dialog_MainWindow.h"
27 #include "Dialog_Processing.h"
28 #include "Thread_Initialization.h"
29 #include "Thread_MessageProducer.h"
30 #include "Model_Settings.h"
31 #include "Model_FileList.h"
32 #include "Model_AudioFile.h"
33 #include "Encoder_Abstract.h"
34 #include "ShellIntegration.h"
35
36 //MUitls
37 #include <MUtils/Global.h>
38 #include <MUtils/OSSupport.h>
39 #include <MUtils/Version.h>
40 #include <MUtils/CPUFeatures.h>
41 #include <MUtils/Terminal.h>
42 #include <MUtils/Startup.h>
43 #include <MUtils/IPCChannel.h>
44
45 //Qt includes
46 #include <QMutex>
47 #include <QApplication>
48 #include <QMessageBox>
49 #include <QDate>
50 #include <QDir>
51
52 //VLD
53 #ifdef _MSC_VER
54 #include <vld.h>
55 #endif
56
57 ///////////////////////////////////////////////////////////////////////////////
58 // Helper functions
59 ///////////////////////////////////////////////////////////////////////////////
60
61 static void lamexp_print_logo(void)
62 {
63         //Print version info
64         qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
65         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()));
66         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());
67         
68         //print license info
69         qDebug("This program is free software: you can redistribute it and/or modify");
70         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
71         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
72
73         //Print library version
74         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)));
75         
76         //Print warning, if this is a "debug" build
77         if(MUTILS_DEBUG)
78         {
79                 qWarning("---------------------------------------------------------");
80                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
81                 qWarning("---------------------------------------------------------\n");
82         }
83 }
84
85 static int lamexp_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
86 {
87         int iResult = 0;
88
89         if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
90         {
91                 if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
92                 {
93                         qDebug("LameXP is already running, connecting to running instance...");
94                         QScopedPointer<MessageProducerThread> messageProducerThread(new MessageProducerThread(ipcChannel));
95                         messageProducerThread->start();
96                         if(!messageProducerThread->wait(30000))
97                         {
98                                 qWarning("MessageProducer thread has encountered timeout -> going to kill!");
99                                 messageProducerThread->terminate();
100                                 messageProducerThread->wait();
101                                 MUtils::OS::system_message_err(L"LameXP", L"LameXP is already running, but the running instance doesn't respond!");
102                                 return -1;
103                         }
104                         return 0;
105                 }
106                 else
107                 {
108                         qFatal("The IPC initialization has failed!");
109                         return -1;
110                 }
111         }
112
113         return 1;
114 }
115
116 static void lamexp_show_splash(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, SettingsModel *const settingsModel)
117 {
118         QScopedPointer<InitializationThread> poInitializationThread(new InitializationThread(cpuFeatures));
119         SplashScreen::showSplash(poInitializationThread.data());
120         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
121 }
122
123 static int lamexp_main_loop(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtils::IPCChannel *const ipcChannel, int &iShutdown)
124 {
125         int iResult = -1;
126         bool bAccepted = true;
127
128         //Create models
129         QScopedPointer<FileListModel>           fileListModel(new FileListModel()          );
130         QScopedPointer<AudioFileModel_MetaInfo> metaInfo     (new AudioFileModel_MetaInfo());
131         QScopedPointer<SettingsModel>           settingsModel(new SettingsModel()          );
132
133         //Show splash screen
134         lamexp_show_splash(cpuFeatures, settingsModel.data());
135
136         //Validate settings
137         settingsModel->validate();
138
139         //Create main window
140         QScopedPointer<MainWindow> poMainWindow(new MainWindow(ipcChannel, fileListModel.data(), metaInfo.data(), settingsModel.data()));
141
142         //Main application loop
143         while(bAccepted && (iShutdown <= SHUTDOWN_FLAG_NONE))
144         {
145                 //Show main window
146                 poMainWindow->show();
147                 iResult = qApp->exec();
148                 bAccepted = poMainWindow->isAccepted();
149
150                 //Sync settings
151                 settingsModel->syncNow();
152
153                 //Show processing dialog
154                 if(bAccepted && (fileListModel->rowCount() > 0))
155                 {
156                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel.data(), metaInfo.data(), settingsModel.data());
157                         processingDialog->exec();
158                         iShutdown = processingDialog->getShutdownFlag();
159                         MUTILS_DELETE(processingDialog);
160                 }
161         }
162
163         return iResult;
164 }
165
166 ///////////////////////////////////////////////////////////////////////////////
167 // Main function
168 ///////////////////////////////////////////////////////////////////////////////
169
170 static int lamexp_main(int &argc, char **argv)
171 {
172         int iResult = -1;
173         int iShutdown = SHUTDOWN_FLAG_NONE;
174
175         //Print logo
176         lamexp_print_logo();
177
178         //Get CLI arguments
179         const MUtils::OS::ArgumentMap &arguments = MUtils::OS::arguments();
180
181         //Enumerate CLI arguments
182         if(!arguments.isEmpty())
183         {
184                 qDebug("Command-Line Arguments:");
185                 foreach(const QString &key, arguments.uniqueKeys())
186                 {
187                         foreach(const QString &val, arguments.values(key))
188                         {
189                                 if(!val.isEmpty())
190                                 {
191                                         qDebug("--%s = \"%s\"", MUTILS_UTF8(key), MUTILS_UTF8(val));
192                                         continue;
193                                 }
194                                 qDebug("--%s", MUTILS_UTF8(key));
195                         }
196                 }
197                 qDebug(" ");
198         }
199
200         //Uninstall?
201         if(arguments.contains("uninstall"))
202         {
203                 qWarning("Un-install: Removing LameXP shell integration...");
204                 ShellIntegration::remove(false);
205                 return EXIT_SUCCESS;
206         }
207
208         //Detect CPU capabilities
209         const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect();
210         qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.vendor, MUTILS_BOOL2STR(cpuFeatures.intel));
211         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
212         qDebug("   CPU signature  :  Family=%d Model=%d Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
213         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));
214         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
215
216         //Initialize Qt
217         QScopedPointer<QApplication> application(MUtils::Startup::create_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")));
218         if(application.isNull())
219         {
220                 return EXIT_FAILURE;
221         }
222
223         //Initialize application
224         application->setWindowIcon(lamexp_app_icon());
225         application->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build())); 
226
227         //Check for expiration
228         if(lamexp_version_demo())
229         {
230                 const QDate currentDate = MUtils::OS::current_date();
231                 if(currentDate.addDays(1) < MUtils::Version::app_build_date())
232                 {
233                         qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), MUtils::Version::app_build_date().toString(Qt::ISODate).toLatin1().constData());
234                 }
235                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
236         }
237
238         //Initialize IPC
239         QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("lamexp-v4", lamexp_version_build(), "instance"));
240         if((iResult = lamexp_initialize_ipc(ipcChannel.data())) < 1)
241         {
242                 return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
243         }
244
245         //Kill application?
246         if(arguments.contains("kill") || arguments.contains("force-kill"))
247         {
248                 return EXIT_SUCCESS;
249         }
250         
251         //Self-test
252         if(MUTILS_DEBUG)
253         {
254                 InitializationThread::selfTest();
255         }
256
257         //Main application loop
258         iResult = lamexp_main_loop(cpuFeatures, ipcChannel.data(), iShutdown);
259
260         //Final clean-up
261         qDebug("Shutting down, please wait...\n");
262
263         //Shotdown computer
264         if(iShutdown > SHUTDOWN_FLAG_NONE)
265         {
266                 if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == SHUTDOWN_FLAG_HIBERNATE)))
267                 {
268                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
269                 }
270         }
271
272         //Terminate
273         return iResult;
274 }
275
276 ///////////////////////////////////////////////////////////////////////////////
277 // Applicaton entry point
278 ///////////////////////////////////////////////////////////////////////////////
279
280 int main(int argc, char* argv[])
281 {
282         return MUtils::Startup::startup(argc, argv, lamexp_main, "LameXP", lamexp_version_demo());
283 }