Implement multi-theaded processing in progress dialog.
[LameXP.git] / src / Main.cpp
blob9abc5f5defb4491a667f18efd9c1a3cbb9ef4f61
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
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"
33 //Qt includes
34 #include <QApplication>
35 #include <QMessageBox>
36 #include <QDate>
37 #include <QMutex>
39 ///////////////////////////////////////////////////////////////////////////////
40 // Main function
41 ///////////////////////////////////////////////////////////////////////////////
43 int lamexp_main(int argc, char* argv[])
45 int iResult = -1;
46 bool bAccepted = true;
48 //Init console
49 lamexp_init_console(argc, argv);
51 //Print version info
52 qDebug("LameXP - Audio Encoder Front-End");
53 qDebug("Version %d.%02d %s, Build %d [%s], MSVC compiler v%02d.%02d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), _MSC_VER / 100, _MSC_VER % 100);
54 qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
56 //print license info
57 qDebug("This program is free software: you can redistribute it and/or modify");
58 qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
59 qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
61 //Print warning, if this is a "debug" build
62 LAMEXP_CHECK_DEBUG_BUILD;
64 //Detect CPU capabilities
65 lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
66 qDebug("CPU brand string: %s", cpuFeatures.brand);
67 qDebug("CPU signature: Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
68 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));
69 qDebug("CPU no. of cores: %d\n", cpuFeatures.count);
71 //Initialize Qt
72 lamexp_init_qt(argc, argv);
74 //Check for expiration
75 if(lamexp_version_demo())
77 QDate expireDate = lamexp_version_date().addDays(14);
78 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(expireDate.toString(Qt::ISODate)).toLatin1().constData());
79 if(QDate::currentDate() >= expireDate)
81 qWarning("Binary has expired !!!");
82 QMessageBox::warning(NULL, "LameXP - Expired", QString("This demo (pre-release) version of LameXP has expired at %1.\nLameXP is free software and release versions won't expire.").arg(expireDate.toString()), "Exit Program");
83 return 0;
87 //Check for multiple instances of LameXP
88 if((iResult = lamexp_init_ipc()) != 0)
90 qDebug("LameXP is already running, connecting to running instance...");
91 if(iResult == 1)
93 MessageProducerThread *messageProducerThread = new MessageProducerThread();
94 messageProducerThread->start();
95 if(!messageProducerThread->wait(30000))
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;
104 LAMEXP_DELETE(messageProducerThread);
106 return 0;
109 //Kill application?
110 for(int i = 0; i < argc; i++)
112 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
114 return 0;
118 //Create models
119 FileListModel *fileListModel = new FileListModel();
120 AudioFileModel *metaInfo = new AudioFileModel();
121 SettingsModel *settingsModel = new SettingsModel();
122 settingsModel->validate();
124 //Show splash screen
125 InitializationThread *poInitializationThread = new InitializationThread();
126 SplashScreen::showSplash(poInitializationThread);
127 LAMEXP_DELETE(poInitializationThread);
129 //Show main window
130 while(bAccepted)
132 MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
133 poMainWindow->show();
134 iResult = QApplication::instance()->exec();
135 bAccepted = poMainWindow->isAccepted();
136 LAMEXP_DELETE(poMainWindow);
138 //Show processing dialog
139 if(bAccepted && fileListModel->rowCount() > 0)
141 ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel);
142 processingDialog->exec();
143 LAMEXP_DELETE(processingDialog);
147 //Free models
148 LAMEXP_DELETE(fileListModel);
149 LAMEXP_DELETE(metaInfo);
150 LAMEXP_DELETE(settingsModel);
152 //Final clean-up
153 qDebug("Shutting down, please wait...\n");
155 //Terminate
156 return iResult;
159 ///////////////////////////////////////////////////////////////////////////////
160 // Applicaton entry point
161 ///////////////////////////////////////////////////////////////////////////////
163 int main(int argc, char* argv[])
167 int iResult;
168 qInstallMsgHandler(lamexp_message_handler);
169 LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
170 lamexp_finalization();
171 return iResult;
173 catch(char *error)
175 fflush(stdout);
176 fflush(stderr);
177 fprintf(stderr, "\nEXCEPTION ERROR: %s\n", error);
178 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
179 TerminateProcess(GetCurrentProcess(), -1);
181 catch(int error)
183 fflush(stdout);
184 fflush(stderr);
185 fprintf(stderr, "\nEXCEPTION ERROR: Error code 0x%X\n", error);
186 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
187 TerminateProcess(GetCurrentProcess(), -1);
189 catch(...)
191 fflush(stdout);
192 fflush(stderr);
193 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
194 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
195 TerminateProcess(GetCurrentProcess(), -1);