OSDN Git Service

Updated Monkey's Audio binary to v4.10 (2011-04-16).
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 //LameXP includes
23 #include "Global.h"
24 #include "Dialog_SplashScreen.h"
25 #include "Dialog_MainWindow.h"
26 #include "Dialog_Processing.h"
27 #include "Thread_Initialization.h"
28 #include "Thread_MessageProducer.h"
29 #include "Model_Settings.h"
30 #include "Model_FileList.h"
31 #include "Model_AudioFile.h"
32 #include "Encoder_Abstract.h"
33 #include "WinSevenTaskbar.h"
34
35 //Qt includes
36 #include <QApplication>
37 #include <QMessageBox>
38 #include <QDate>
39 #include <QMutex>
40 #include <QDir>
41 #include <QInputDialog>
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // Main function
45 ///////////////////////////////////////////////////////////////////////////////
46
47 static int lamexp_main(int argc, char* argv[])
48 {
49         int iResult = -1;
50         int iShutdown = shutdownFlag_None;
51         bool bAccepted = true;
52         
53         //Init console
54         lamexp_init_console(argc, argv);
55
56         //Print version info
57         qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
58         qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(lamexp_version_date().year(),QDate::currentDate().year()));
59         qDebug("Built on %s at %s with %s for Win-%s.\n", lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_time(), lamexp_version_compiler(), lamexp_version_arch());
60         
61         //print license info
62         qDebug("This program is free software: you can redistribute it and/or modify");
63         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
64         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
65
66         //Print warning, if this is a "debug" build
67         if(LAMEXP_DEBUG)
68         {
69                 qWarning("---------------------------------------------------------");
70                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
71                 qWarning("---------------------------------------------------------\n"); 
72         }
73         
74         //Detect CPU capabilities
75         lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(argc, argv);
76         qDebug("   CPU vendor id  :  %s (Intel: %s)", cpuFeatures.vendor, LAMEXP_BOOL(cpuFeatures.intel));
77         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
78         qDebug("   CPU signature  :  Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
79         qDebug("CPU capabilities  :  MMX: %s, SSE: %s, SSE2: %s, SSE3: %s, SSSE3: %s, x64: %s", LAMEXP_BOOL(cpuFeatures.mmx), LAMEXP_BOOL(cpuFeatures.sse), LAMEXP_BOOL(cpuFeatures.sse2), LAMEXP_BOOL(cpuFeatures.sse3), LAMEXP_BOOL(cpuFeatures.ssse3), LAMEXP_BOOL(cpuFeatures.x64));
80         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
81         
82         //Initialize Qt
83         if(!lamexp_init_qt(argc, argv))
84         {
85                 return -1;
86         }
87
88         //Check for expiration
89         if(lamexp_version_demo())
90         {
91                 if(QDate::currentDate().addDays(1) < lamexp_version_date())
92                 {
93                         qFatal("System's date (%s) is before LameXP build date (%s). Huh?", QDate::currentDate().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData());
94                 }
95                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
96         }
97
98         //Check for multiple instances of LameXP
99         if((iResult = lamexp_init_ipc()) != 0)
100         {
101                 qDebug("LameXP is already running, connecting to running instance...");
102                 if(iResult == 1)
103                 {
104                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
105                         messageProducerThread->start();
106                         if(!messageProducerThread->wait(30000))
107                         {
108                                 messageProducerThread->terminate();
109                                 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "LameXP is already running, but the running instance doesn't respond!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
110                                 messageBox.exec();
111                                 messageProducerThread->wait();
112                                 LAMEXP_DELETE(messageProducerThread);
113                                 return -1;
114                         }
115                         LAMEXP_DELETE(messageProducerThread);
116                 }
117                 return 0;
118         }
119
120         //Kill application?
121         for(int i = 0; i < argc; i++)
122         {
123                 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
124                 {
125                         return 0;
126                 }
127         }
128         
129         //Self-test
130         if(LAMEXP_DEBUG)
131         {
132                 InitializationThread::selfTest();
133         }
134
135         //Taskbar init
136         WinSevenTaskbar::init();
137
138         //Create models
139         FileListModel *fileListModel = new FileListModel();
140         AudioFileModel *metaInfo = new AudioFileModel();
141         SettingsModel *settingsModel = new SettingsModel();
142         
143         //Show splash screen
144         InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
145         SplashScreen::showSplash(poInitializationThread);
146         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
147         LAMEXP_DELETE(poInitializationThread);
148
149         //Validate settings
150         settingsModel->validate();
151
152         //Create main window
153         MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
154         
155         //Main application loop
156         while(bAccepted && (iShutdown <= shutdownFlag_None))
157         {
158                 //Show main window
159                 poMainWindow->show();
160                 iResult = QApplication::instance()->exec();
161                 bAccepted = poMainWindow->isAccepted();
162
163                 //Show processing dialog
164                 if(bAccepted && (fileListModel->rowCount() > 0))
165                 {
166                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
167                         processingDialog->exec();
168                         iShutdown = processingDialog->getShutdownFlag();
169                         LAMEXP_DELETE(processingDialog);
170                 }
171         }
172         
173         //Free models
174         LAMEXP_DELETE(poMainWindow);
175         LAMEXP_DELETE(fileListModel);
176         LAMEXP_DELETE(metaInfo);
177         LAMEXP_DELETE(settingsModel);
178
179         //Taskbar un-init
180         WinSevenTaskbar::uninit();
181
182         //Final clean-up
183         qDebug("Shutting down, please wait...\n");
184
185         //Shotdown computer
186         if(iShutdown > shutdownFlag_None)
187         {
188                 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
189                 {
190                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
191                 }
192         }
193
194         //Terminate
195         return iResult;
196 }
197
198 ///////////////////////////////////////////////////////////////////////////////
199 // Applicaton entry point
200 ///////////////////////////////////////////////////////////////////////////////
201
202 static int _main(int argc, char* argv[])
203 {
204         if(LAMEXP_DEBUG)
205         {
206                 int iResult = -1;
207                 qInstallMsgHandler(lamexp_message_handler);
208                 LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
209                 lamexp_finalization();
210                 return iResult;
211         }
212         else
213         {
214                 int iResult = -1;
215                 try
216                 {
217                         qInstallMsgHandler(lamexp_message_handler);
218                         iResult = lamexp_main(argc, argv);
219                         lamexp_finalization();
220                 }
221                 catch(char *error)
222                 {
223                         fflush(stdout);
224                         fflush(stderr);
225                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
226                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
227                         TerminateProcess(GetCurrentProcess(), -1);
228                 }
229                 catch(int error)
230                 {
231                         fflush(stdout);
232                         fflush(stderr);
233                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
234                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
235                         TerminateProcess(GetCurrentProcess(), -1);
236                 }
237                 catch(...)
238                 {
239                         fflush(stdout);
240                         fflush(stderr);
241                         fprintf(stderr, "\nGURU MEDITATION !!!\n");
242                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
243                         TerminateProcess(GetCurrentProcess(), -1);
244                 }
245                 return iResult;
246         }
247 }
248
249 int main(int argc, char* argv[])
250 {
251         if(LAMEXP_DEBUG)
252         {
253                 return _main(argc, argv);
254         }
255         else
256         {
257                 __try
258                 {
259                         SetUnhandledExceptionFilter(lamexp_exception_handler);
260                         _set_invalid_parameter_handler(lamexp_invalid_param_handler);
261                         return _main(argc, argv);
262                 }
263                 __except(1)
264                 {
265                         fflush(stdout);
266                         fflush(stderr);
267                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
268                         FatalAppExit(0, L"Unhandeled structured exception error, application will exit!");
269                         TerminateProcess(GetCurrentProcess(), -1);
270                 }
271         }
272 }
273
274 ///////////////////////////////////////////////////////////////////////////////
275 // CRT initialization
276 ///////////////////////////////////////////////////////////////////////////////
277
278 extern "C"
279 {
280         int WinMainCRTStartup(void);
281
282         int lamexp_crt_startup(void)
283         {
284                 return WinMainCRTStartup();
285         }
286 }