OSDN Git Service

7b4de7b6f69840391d4d4d250fa8081790a22e8a
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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
34 //Qt includes
35 #include <QApplication>
36 #include <QMessageBox>
37 #include <QDate>
38 #include <QMutex>
39 #include <QDir>
40 #include <QInputDialog>
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // Main function
44 ///////////////////////////////////////////////////////////////////////////////
45
46 int lamexp_main(int argc, char* argv[])
47 {
48         int iResult = -1;
49         bool bAccepted = true;
50         
51         //Init console
52         lamexp_init_console(argc, argv);
53
54         //Print version info
55         qDebug("LameXP - Audio Encoder Front-End");
56         qDebug("Version %d.%02d %s, Build %d [%s], compiled with %s", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_compiler());
57         qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
58         
59         //print license info
60         qDebug("This program is free software: you can redistribute it and/or modify");
61         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
62         qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
63
64         //Print warning, if this is a "debug" build
65         LAMEXP_CHECK_DEBUG_BUILD;
66         
67         //Detect CPU capabilities
68         lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
69         qDebug("   CPU vendor id  :  %s (Intel: %d)", cpuFeatures.vendor, (cpuFeatures.intel ? 1 : 0));
70         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
71         qDebug("   CPU signature  :  Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
72         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));
73         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
74         
75         //Initialize Qt
76         if(!lamexp_init_qt(argc, argv))
77         {
78                 return -1;
79         }
80
81         //Check for expiration
82         if(lamexp_version_demo())
83         {
84                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
85         }
86
87         //Check for multiple instances of LameXP
88         if((iResult = lamexp_init_ipc()) != 0)
89         {
90                 qDebug("LameXP is already running, connecting to running instance...");
91                 if(iResult == 1)
92                 {
93                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
94                         messageProducerThread->start();
95                         if(!messageProducerThread->wait(30000))
96                         {
97                                 messageProducerThread->terminate();
98                                 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);
99                                 messageBox.exec();
100                                 messageProducerThread->wait();
101                                 LAMEXP_DELETE(messageProducerThread);
102                                 return -1;
103                         }
104                         LAMEXP_DELETE(messageProducerThread);
105                 }
106                 return 0;
107         }
108
109         //Kill application?
110         for(int i = 0; i < argc; i++)
111         {
112                 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
113                 {
114                         return 0;
115                 }
116         }
117         
118         //Create models
119         FileListModel *fileListModel = new FileListModel();
120         AudioFileModel *metaInfo = new AudioFileModel();
121         SettingsModel *settingsModel = new SettingsModel();
122         
123         //Show splash screen
124         InitializationThread *poInitializationThread = new InitializationThread();
125         SplashScreen::showSplash(poInitializationThread);
126         LAMEXP_DELETE(poInitializationThread);
127
128         //Validate settings
129         settingsModel->validate();
130
131         //Create main window
132         MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
133         
134         //Main application loop
135         while(bAccepted)
136         {
137                 //Show main window
138                 poMainWindow->show();
139                 iResult = QApplication::instance()->exec();
140                 bAccepted = poMainWindow->isAccepted();
141
142                 //Show processing dialog
143                 if(bAccepted && fileListModel->rowCount() > 0)
144                 {
145                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
146                         processingDialog->exec();
147                         LAMEXP_DELETE(processingDialog);
148                 }
149         }
150         
151         //Free models
152         LAMEXP_DELETE(poMainWindow);
153         LAMEXP_DELETE(fileListModel);
154         LAMEXP_DELETE(metaInfo);
155         LAMEXP_DELETE(settingsModel);
156
157         //Final clean-up
158         qDebug("Shutting down, please wait...\n");
159
160         //Terminate
161         return iResult;
162 }
163
164 ///////////////////////////////////////////////////////////////////////////////
165 // Applicaton entry point
166 ///////////////////////////////////////////////////////////////////////////////
167
168 int main(int argc, char* argv[])
169 {
170 #ifdef _DEBUG
171         int iResult;
172         qInstallMsgHandler(lamexp_message_handler);
173         LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
174         lamexp_finalization();
175         return iResult;
176 #else
177         try
178         {
179                 int iResult;
180                 qInstallMsgHandler(lamexp_message_handler);
181                 iResult = lamexp_main(argc, argv);
182                 lamexp_finalization();
183                 return iResult;
184         }
185         catch(char *error)
186         {
187                 fflush(stdout);
188                 fflush(stderr);
189                 fprintf(stderr, "\nGURU MEDITATION: %s\n", error);
190                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
191                 TerminateProcess(GetCurrentProcess(), -1);
192         }
193         catch(int error)
194         {
195                 fflush(stdout);
196                 fflush(stderr);
197                 fprintf(stderr, "\nGURU MEDITATION: Error code 0x%X\n", error);
198                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
199                 TerminateProcess(GetCurrentProcess(), -1);
200         }
201         catch(...)
202         {
203                 fflush(stdout);
204                 fflush(stderr);
205                 fprintf(stderr, "\nGURU MEDITATION !!!\n");
206                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
207                 TerminateProcess(GetCurrentProcess(), -1);
208         }
209 #endif
210 }
211
212 extern "C"
213 {
214         void __declspec(dllexport) __stdcall Test(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
215         {
216                 OutputDebugStringA("Hello cruel world!");
217                 MessageBoxA(0, lpszCmdLine, "LameXP v9.0", MB_ICONINFORMATION);
218         }
219 }